vue中的 namespaced

21 篇文章 0 订阅
//在vue中会这样配置
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}

状态管理机制 new Vuex.Store()

 

namespaced这是属性用于解决不同模块的命名冲突问题

在不同页面引入getter、actions、mutations时,要加上模块名

 ...mapGetters('BadInfo', [
      'DialogData',
      'ComponentMap',
      'Layout'])
  },

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue的命名空间(Namespaced)是一种将模块化组织的方式,它可以避免模块之间的命名冲突,以及在 Vuex 等状态管理工具使用时,可以更方便地组织和调用模块。在 Vue ,命名空间是通过 `namespaced` 属性来实现的。 在一个模块,如果将 `namespaced` 属性设置为 `true`,那么该模块的所有 getters、mutations 和 actions 都会被自动添加上一个命名空间。例如: ```javascript const store = new Vuex.Store({ modules: { foo: { namespaced: true, state: { count: 0 }, getters: { getCount: state => state.count }, mutations: { increment: state => state.count++, decrement: state => state.count-- }, actions: { asyncIncrement({ commit }) { setTimeout(() => commit('increment'), 1000) } } } } }) ``` 在上面的代码,我们定义了一个名为 `foo` 的模块,并将其设置为命名空间模块。这样,我们在组件使用该模块的 getters、mutations 和 actions 时,需要通过 `namespace` 属性来指定命名空间,例如: ```javascript export default { computed: { count() { return this.$store.getters['foo/getCount'] } }, methods: { increment() { this.$store.commit('foo/increment') }, asyncIncrement() { this.$store.dispatch('foo/asyncIncrement') } } } ``` 需要注意的是,在使用命名空间时,如果不指定命名空间,那么默认会访问全局的 getters、mutations 和 actions,这可能会导致错误。因此,在使用命名空间时,一定要注意指定命名空间。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值