this.$store.commit使用
原文来自:https://blog.csdn.net/wf00334814/article/details/84348654
由于父子组件通信常使用props和$emit,对于平行组件之间改方法比较麻烦,不利于代码复用,因此使用this.$store.commit
(一)安装vuex
(二)创建store文件夹,专门存放相关文件
(三)新建index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store=new Vuex.Store({
state:{
text:{tip:"这是一条数据!"}
},
mutations:{//对state进行修改
modify(state,msg){
state.text.tip="修改数据!"
}
}
})
(4)挂载到实例上,注册
import store from '@/store/index.js'
new Vue({
store,
router
}).$mount("#app")
(5)在组件中使用
this.$store.commit('modify',"改变值!")
let value=this.$store.state.text.tip
作者:EO_eaf6
链接://www.greatytc.com/p/3f695e336f77
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。