当前位置:   article > 正文

调用 vuex modules中的同步方法_vue 调用vuex同步方法

vue 调用vuex同步方法

1,store/index.js

  1. import Vue from 'vue';
  2. import Vuex from 'vuex'
  3. import home from './modules/home.js'
  4. Vue.use(Vuex);
  5. const store = new Vuex.Store({
  6. modules: {
  7. home
  8. }
  9. })
  10. export default store;

2,加命名空间

 namespaced: true,

store/home/home.js

  1. const state = {
  2. indexData: {}
  3. }
  4. const mutations = {
  5. getIndexDataMutations(state, data) {
  6. state.indexData = data
  7. }
  8. }
  9. const actions = {
  10. }
  11. const getters = {}
  12. export default {
  13. //加上命名空间
  14. namespaced: true,
  15. state,
  16. mutations,
  17. actions,
  18. getters
  19. }

3,

只调用home.js下面的mutation方法怎么办?

this.$store.commit("模块名/该模块的方法",传递的参数)

4,在vue文件中调用

  1. created() {
  2. this.$store.commit("home/getIndexDataMutations", {
  3. name: 3
  4. })
  5. console.log(this.$store.state.home.indexData);
  6. },

控制台输出

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/602041
推荐阅读
相关标签
  

闽ICP备14008679号