如何将数据从组件传递到商店Vue?

时间:2018-08-08 12:34:42

标签: javascript vue.js vuejs2

我需要设置一些从组件到商店的数据

    methods: { // my component
          ...mapMutations('lists', ['setContactListName']),//import function from  the store
          viewHandler (id, item) { // hadler
            this.$router.push(`/company/sample-contact/${id}`);
            this.setContactListName(item); // pass data to the function
          }
        }

    state() { // store
        return {
          contactListName: {}
        };
      },
    mutations:{
     setContactListName (state, payload) {// handler
          state.contactListName = payload;
        }
    }

我单击后什么也没有发生,即使控制台中没有错误

1 个答案:

答案 0 :(得分:1)

methods: { // my component
  ...mapMutations(['setContactListName']), //import function from  the store
  viewHandler(id, item) { // hadler
    this.$router.push(`/company/sample-contact/${id}`);
    this.setContactListName(item); // pass data to the function
  }
}

或使用 this。$ store.commit

methods: { // my component
  viewHandler(id, item) { // hadler
    this.$router.push(`/company/sample-contact/${id}`);
    this.$store.commit('setContactListName', item);
  }
}