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. Shiro笔记(五)Shiro授权

    Shiro授权 也叫访问控制,即在应用中控制谁能访问那些资源(如访问页面.编辑数据.页面操作等).在授权中需要了解几个关键对象:主体(subject).资源(resource).权限(Permissi ...

  2. ELM:ELM实现鸢尾花种类测试集预测识别正确率(better)结果对比—Jason niu

    load iris_data.mat P_train = []; T_train = []; P_test = []; T_test = []; for i = 1:3 temp_input = fe ...

  3. Jquery操作一遍过

    什么是jQuery对象? jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQu ...

  4. 【Spring Boot】使用JDBC 获取相关的数据

    使用JDBC 获取相关的数据 什么是JDBC Java Database Connectivity 是一种用于执行SQL语句的Java API,与数据库建立连接.发送 操作数据库的语句并处理结果. S ...

  5. springboot2.0 redis EnableCaching的配置和使用

    一.前言 关于EnableCaching最简单使用,个人感觉只需提供一个CacheManager的一个实例就好了.springboot为我们提供了cache相关的自动配置.引入cache模块,如下. ...

  6. spring_AOP_XML

    例子下载 对于xml的AOP配置主要集中在配置文件中,所以只要设置好配置文件就行了 beans.xml <?xml version="1.0" encoding=" ...

  7. Maven使用lib下的包

    Maven使用中央仓库的同时,使用lib下的包 pom.xml添加如下配置 <build> <plugins> <plugin> <artifactId> ...

  8. [iOS]视图与UIVIew

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

  9. VeeamOne9.5-t添加监控服务器

    打开 Veeam ONE Monitor 首先会让你配置报警邮件,也可以选择跳过随后配置 点击ADD SERVER 可以选择vCenter也可以选择ESXI主机 输入vCenter的用户名和密码 点击 ...

  10. BZOJ.4540.[HNOI2016]序列(莫队/前缀和/线段树 单调栈 RMQ)

    BZOJ 洛谷 ST表的一二维顺序一定要改过来. 改了就rank1了哈哈哈哈.自带小常数没办法. \(Description\) 给定长为\(n\)的序列\(A_i\).\(q\)次询问,每次给定\( ...