vuex复习

Vue.use(Vuex)

  new Vue({

        store:store

   })

子组件可以通过$store访问

 state mapState 

computed:{

        ...mapState({

            count:state=>state.count

    })

}

getters  store.getters.count

            getters:{

                        count(state,getters){  参数一是state,参数二是getters

                        }

        }

        可以返回一个函数,就可以带参数

             getters:{

                    count(state)=>(arg)=>{

                return {}

        }

        computed:{

                ...mapGetters([""])

        }

Mutation

        $store.commit("name",value)

        methods:{

                   ...mapMutations({

                    })    

        }

Actions

       actions:{

            increment(context,arg){   参数一store实例对象本身

                    context.commit()

            }    

        }

    $store.dispach()

    mapActions

modules 分模块

new Vuex.Store({

        state:{},

         modules:{

                user:{

                        state:{

                        }

                }

        }

})

   ...mapState({

            user: (state)=>根模块直接点 state.attrname    子模块 state[moduleA].attrname

    })

...mapGetters({

        getter:""   没开启命名空间的时候  getters不区分根模块还是子模块,直接写getters名称就能找到,开启了需要

})

actions(context)参数一会暴露根模块的rootState的仓库数据

getters(state,getters,rootState) 参数三为根模块的仓库数据

命名空间

        如果希望你的模块具有更高的封装度和复用性,你可以通过添加 namespaced: true 的方式使其成为带命名空间的模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名

        子模块访问全局getters

        getters:{

                moduleGetter(state,getters,rootState,rootGetters){

        }

        }

actions(context) context也带有rootGetters参数

在子模块中触发根模块的方法

       actions:{

                actionFun({dispach}){

                        dispach("根组件方法",null,{root:true})

                }

        }

   在子组件中定义全局actions

        actions:{

                actionfun:{

                        root:true,

                        handler:()=>{


                        }

                }

        }



模块动态注册

        store.registerModule()

            

store插件

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 8,040评论 0 7
  • vuex vue 专用 状态管理模式 采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的...
    前端菜菜1号阅读 3,025评论 0 0
  • 配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...
    sunny688阅读 6,882评论 0 23
  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 4,042评论 0 3
  • 配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...
    aYaaWa阅读 1,513评论 0 1

友情链接更多精彩内容