[Vuex] Use Namespaces in Vuex Stores using TypeScript
Even by using modules, they still share the same namespace. So you couldn’t have the same mutation name in different modules. Namespaces solve that by prepending the path of the module to the State
, Getter
, Action
, and Mutation
.
This lesson shows how to use namespaces in Vuex modules with TypeScript.
Enable namespaced to each feature store:
import {GetterTree, MutationTree, Module} from 'vuex'; import {TodosState, RootState} from '../types'; import {Todo} from '../types'; const state: TodosState = { todos: [ {text: 'Buy milk', checked: false}, {text: 'Learning', checked: true}, {text: 'Algorithom', checked: false}, ], }; const getters: GetterTree<TodosState, RootState> = { todos: (state, getters, rootState) => state.todos.filter(t => !t.checked), dones: (state, getters, rootState) => state.todos.filter(t => t.checked) }; const mutations: MutationTree<TodosState> = { addTodo(state, newTodo) { const copy = { ...newTodo }; state.todos.push(copy); }, toggleTodo(TodosState, todo) { todo.checked = !todo.checked; } }; const actions: ActionTree<TodosState, RootState> = { addTodoAsync(context, id) { fetch('https://jsonplaceholder.typicode.com/posts/'+id) .then(data => data.json()) .then(item => { const todo: Todo = { checked: false, text: context.rootState.login.user + ': ' + item.title } context.commit('addTodo', todo); }) } } export const todos: Module<TodosState, RootState> = { actions, state, mutations, getters, namespaced: true };
Now we need to modify the component to adopt the namespace:
<template> <section> <h4>Todos</h4> <ul> <li v-for="todo in todos"> <input type="checkbox" :checked="todo.checked" @change="toggleTodo(todo)"> {{ todo.text }} </li> </ul> <h4>Done</h4> <ul> <li v-for="todo in dones"> <input type="checkbox" :checked="todo.checked" @change="toggleTodo(todo)"> {{ todo.text }} </li> </ul> <p> <label for=""> Add todo: <input type="text" v-model="newTodo.text" @keyup.enter="addTodo(newTodo)"> </label> <label for=""> Add todo async: <input type="text" v-model="id" @keyup.enter="addTodoAsync(id)"> </label> </p> </section> </template> <script lang="ts"> import {Component, Vue} from 'vue-property-decorator'; import {Action, State, Getter, Mutation, namespace} from 'vuex-class'; import {TodosState, Todo} from '../types'; const TodoGetter = namespace('todos', Getter); const TodoAction = namespace('todos', Action); const TodoMutation = namespace('todos', Mutation); @Component({ }) export default class Todos extends Vue { @TodoGetter todos: TodosState; @TodoGetter dones: TodosState; @TodoMutation addTodo; @TodoMutation toggleTodo; @TodoAction addTodoAsync; newTodo: Todo = { text: '', checked: false } id: string = '1'; } </script>
If we want to trigger a different namesapce state update from Todo State, we can do it in action:
const actions: ActionTree<TodosState, RootState> = { addTodoAsync(context, id) { fetch('https://jsonplaceholder.typicode.com/posts/'+id) .then(data => data.json()) .then(item => { const todo: Todo = { checked: false, text: context.rootState.login.user + ': ' + item.title } context.commit('addTodo', todo); context.commit('login/login', null, {root: true}); // we have to say {root: true}, otherwise, it will look for the state under 'todos' namespace }) } }
Login mutation:
const mutations: MutationTree<LoginState> = { login (state) { state.isLoggedIn = true; state.user = 'alice'; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2017-04-29 [Node.js] Test Node RESTful API with Mocha and Chai
2016-04-29 [Typescript] Introduction to Generics in Typescript
2015-04-29 [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2
2015-04-29 [Whole Web, Node.js PM2] Loggin with PM2
2015-04-29 [Whole Web, Node.js, PM2] Configuring PM2 for Node applications
2015-04-29 [ES6] 22. Const
2015-04-29 [ES6] 21. ESNext, ES6-Shim & Node