vuex 监听数组或对象

2021-08-18 15:50:16 浏览数 (1)

store.js

代码语言:javascript复制
/*
 * @Author: Zheng Lei
 * @Email: baimoc@163.com
 * @Date: 2020-06-14 09:47:03
 * @LastEditTime: 2021-08-12 17:20:02
 * @FilePath: EmAtlassrcstore.js
 */
import Vue from 'vue'
import Vuex from "vuex"

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        obj_data: [],
    },
    mutations: {
        objMutation(state, data){
            state.obj_data = JSON.parse(JSON.stringify(data))
        },
    },
})

A组件

代码语言:javascript复制
this.$store.commit('objMutation', current_obj_data);

B组件

代码语言:javascript复制
watch: {
    '$store.state.obj_data'(val){
        console.log(val);
    },
},

0 人点赞