vuex 命名空间

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

export default moduleA={

  namespaced:true

}

带命名空间的绑定函数

computed:{

  ...mapstate({

    a:state=>state.namespace.a
  });

}

也可以将模块的空间名称字符串作为第一个参数传递给上述的函数,这样所有的绑定就会自动将该模块作为上下文,

computed:{

  ...mapstate(namespace,{

    a:state=>state.a
  });

}

也可以通过createNamespaceHelpers 创建基于某个命名空间辅助函数。它返回一个对象,对象里有新的绑定在给定命名空间的组件绑定的辅助函数:

import {createNamespaceHelpers} from ‘vuex’

const {mapstate}=createNamespaceHelplers(namespace);

computed:{

  ...mapstate({

    a:state=>state.a
   });

}
posted on 2019-02-26 10:53  半夏微澜ぺ  阅读(433)  评论(0编辑  收藏  举报