Skip to content

New module option namespace #380

Closed
Closed
@ktsn

Description

@ktsn

Last update: 2016-10-23

This proposal is come up from the discussion in #359.

Summary of proposal

Modules will have namespace option that expects string value. All getters, actions and mutations types in the module are prefixed by the given value.

Namespace is inherited to child modules implicitly and we can also nest namespace by declaring namespace option on the child modules.

Also, this option can be more useful with subStore option that is another new module option proposal #381.

Why is this worth adding?

Currently Vuex does not auto-namespace getters, actions and mutations even though they are defined in some nested modules, the namespacing is handled by developers. This design is reasonable because there are some cases that the auto-namespacing will suffer developers as discussed in #236 .

However, I think it would be useful that we have optional auto-namespacing because we probably want to prefix all getters, actions and mutations type in the same module in most cases.

Examples

export default {
  namespace: 'user/',

  // module assets
  state: { ... }, // module state will not be changed by prefix option
  getters: {
    isAdmin () { ... } // -> getters['user/isAdmin']
  },
  actions: {
    login () { ... } // -> dispatch('user/login')
  },
  mutations: {
    setName () { ... } // -> commit('user/setName')
  },

  // nested modules
  modules: {
    // inherit the prefix from parent module
    nestedA: {
      state: { ... },
      getters: {
        nestedGetterA () { ... } // -> getters['user/nestedGetterA']
      }
    },

    // nest the namespace
    nestedB: {
      namespace: 'nestedB/',

      state: { ... },
      getters: {
        nestedGetterB () { ... } // -> getters['user/nestedB/nestedGetterB']
      }
    }
  }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions