VueX中 设置namespaced: true后 mutations不生效问题

在组件中使用 this.$store.commit('xxx',xxx); 发现控制台一直报错,检查命名后发现也没有错误

后不经意间发现代码中有一个陌生的字眼namespaced: true,在网上对它进行搜索后得以解决

原来vuex中的store分模块管理时,需要在store的index.js中引入各个模块,为了解决不同模块命名冲突的问题,将不同模块的namespaced:true,之后在不同页面中引入getter、actions、mutations时,需要加上所属的模块名

那么当namespaced: true如何获取使用vuex中的数据?

state

//获取。不管为true为false
this.$store.state.schedule.pages
//辅助函数
...mapState({
    pages: state=>state.home.pages,
}),

getters

//获取
this.$store.getters['schedule/changepg']
//辅助函数
...mapGetters({
    total:'schedule/changepg'
})

mutations

//commit调用mutation更新
changepg(){
    this.$store.commit('home/changepg',5);
 }
 //辅助函数
 ...mapMutations({
    changepg: 'home/changepg',
 })
 

actions

//dispatch派发actions
change(){
    this.$store.dispatch('home/changepg',5)
}
//辅助函数
...mapActions({
    changepg: 'home/changepg',
}),

也就是在methods中需要写成这种格式才得以解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值