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 StateGetterAction, 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';
}
}

[Vuex] Use Namespaces in Vuex Stores using TypeScript的更多相关文章

  1. [Vuex] Lazy Load a Vuex Module at Runtime using TypeScript

    Sometimes we need to create modules at runtime, for example depending on a condition. We could even ...

  2. 浅谈vuex使用方法(vuex简单实用方法)

    Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vu ...

  3. 用混入的方法引入vuex,并且解决vuex刷新页面值丢失的问题

    前段时间,做了个官网项目,客户要求将首页的域名后面的参数去除干净,然后就把#去掉了,一转脸,客户让去掉子页面地址栏上的参数,这很棘手,因为子页面的内容是根据子页面地址栏上的参数而定的,如果要去掉这些参 ...

  4. [Vuex系列] - 初尝Vuex第一个例子

    Vuex是什么? Vuex是一个专为vue.js应用程序开发的状态管理库.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 通过定义和隔离状态管理中的各种概 ...

  5. VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers

    数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...

  6. vuex-pathify 一个基于vuex进行封装的 vuex助手语法插件

    首先介绍一下此插件 我们的目标是什么:干死vuex 我来当皇上!(开个玩笑,pathify的是为了简化vuex的开发体验) 插件作者 davestewart github仓库地址 官方网站,英文 说一 ...

  7. 如何在组件中监听vuex数据变化(当vuex中state变化时,子组件需要进行更新,怎么做?)

    todo https://blog.csdn.net/qq_37899792/article/details/97640434

  8. mutation中修改state中的状态值,却报[vuex] do not mutate vuex store state outside mutation handlers.

    网上百度说是在mutation外修改state中的状态值,会报下列错误,可我明明在mutations中修改的状态值,还是报错 接着百度,看到和我类似的问题,说mutations中只能用同步代码,异步用 ...

  9. [Vuex] Split Vuex Store into Modules using TypeScript

    When the Vuex store grows, it can have many mutations, actions and getters, belonging to different c ...

随机推荐

  1. 关于ubuntu的ssh远程登录的问题

    一. 安装ssh(参考:http://liuyifan789.iteye.com/blog/2068263) sudo apt-get install openssh-server openssh-c ...

  2. sql 将一列一逗号分隔拼成字符串

    select stuff((select ','+w.Waybillno from Web_Way_Waybill w where w.IsValid<>'Y' AND w.TruckOr ...

  3. Nightmare Ⅱ HDU - 3085 (双向bfs)

    Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were tra ...

  4. Philosopher’s Walk(递归)

    In Programming Land, there are several pathways called Philosopher’s Walks for philosophers to have ...

  5. 002.Ceph安装部署

    一 前期准备 1.1 配置规格 节点 类型 IP CPU 内存 ceph-deploy 部署管理平台 172.24.8.71 2 C 4 G node1 Monitor OSD 172.24.8.72 ...

  6. XamarinEssentials教程清空键值

    XamarinEssentials教程清空键值 Preferences类的Clear()方法可以清空所有的键和值.该方法有两种形式,下面依次进行介绍. (1)Clear()方法的语法形式如下: pub ...

  7. [iOS]视图与UIVIew

     1.UIView以及各控件间的关系: 2.视图的层次结构 一般来说一个应用中只有一个UIWindow.

  8. ES6快速入门(三)类与模块

    类与模块 一.类 一)类的声明 class Person { constructor(name) { this.name = name; } sayName() { console.log(this. ...

  9. 深入浅出 SVG

    前言 据悉,8月18号将在广州举办中国第一届React开发者大会.今日早读文章由@Starrier翻译分享. 正文从这开始- SVG 是优秀且令人难以置信的强大图像格式.本教程通过简单地解释所有需要了 ...

  10. spring项目出现无法加载主类

    问题:eclipse总是运行之前的项目,新改变的项目内容,不运行.于是我想要是清理缓存,网上说project--->clean指定的项目就可以 但是clean后就无法加载主类了,项目上还出现了红 ...