vue3 项目添加vuex 进行组件中传递数据

添加依赖

yarn add vuex -S

新建 store/index.js文件

import {createStore} from "vuex"

export default createStore({
    state: {
        isCollapse: true,
    },
    mutations: {
        updateCollapse(state, payload) {
            state.isCollapse = !state.isCollapse
        }
    }
});

注册到main.js

 a组件使用属性

 b组件点击更新a组件的数值

 注意 commit里面的参数就是

store/index.js

中的 

mutations 中的  
updateCollapse方法

commit 如果后面再提交参数

那么就是通过payload来进行接受

 

猜你喜欢

转载自blog.csdn.net/mp624183768/article/details/128430756