vuex里mutations定义的方法,在模板里怎么使用

我模板可以直接接调用SET_PLANT方法吗?

vuex

 mutations: {
        SET_PLANT: (state, data) => {
            state.factoryRole = data;
            console.log('vuex');
            console.log(state.factoryRole);
        }
     }
阅读 3.9k
2 个回答

vue文件中使用

import { mapMutations } from 'vuex' // 先引入mapMutations 

methods: {
  ...mapMutations(['SET_PLANT']),
  testFun () {
    let data = ''
    this.SET_PLANT(data) // 使用
  }
}

其他JS文件中使用

import store from '@/store' // 引入store
let data = ''
store.commit('SET_PLANT', data) // 使用

this.$store.commit('SET_PLANT', data);

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题