[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';
}
}
[Vuex] Use Namespaces in Vuex Stores using TypeScript的更多相关文章
- [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 ...
- 浅谈vuex使用方法(vuex简单实用方法)
Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集成到 Vu ...
- 用混入的方法引入vuex,并且解决vuex刷新页面值丢失的问题
前段时间,做了个官网项目,客户要求将首页的域名后面的参数去除干净,然后就把#去掉了,一转脸,客户让去掉子页面地址栏上的参数,这很棘手,因为子页面的内容是根据子页面地址栏上的参数而定的,如果要去掉这些参 ...
- [Vuex系列] - 初尝Vuex第一个例子
Vuex是什么? Vuex是一个专为vue.js应用程序开发的状态管理库.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 通过定义和隔离状态管理中的各种概 ...
- VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers
数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...
- vuex-pathify 一个基于vuex进行封装的 vuex助手语法插件
首先介绍一下此插件 我们的目标是什么:干死vuex 我来当皇上!(开个玩笑,pathify的是为了简化vuex的开发体验) 插件作者 davestewart github仓库地址 官方网站,英文 说一 ...
- 如何在组件中监听vuex数据变化(当vuex中state变化时,子组件需要进行更新,怎么做?)
todo https://blog.csdn.net/qq_37899792/article/details/97640434
- mutation中修改state中的状态值,却报[vuex] do not mutate vuex store state outside mutation handlers.
网上百度说是在mutation外修改state中的状态值,会报下列错误,可我明明在mutations中修改的状态值,还是报错 接着百度,看到和我类似的问题,说mutations中只能用同步代码,异步用 ...
- [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 ...
随机推荐
- BZOJ2724 [Violet 6]蒲公英 分块
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2724.html 题目传送门 - BZOJ2724 题意 求区间最小众数,强制在线. $n$ 个数,$m ...
- Hadoop |集群的搭建
Hadoop组成 HDFS(Hadoop Distributed File System)架构概述 NameNode目录--主刀医生(nn): DataNode(dn)数据: Secondary N ...
- 053 kafka自带的生产者与消费者测试
1.命令 2.启动生产者 bin/kafka-console-producer.sh --topic beifeng --broker-list linux-hadoop01.ibeifeng.com ...
- mac docker环境搭建mysql主从同步服务器
参考地址:https://www.cnblogs.com/jinjiangongzuoshi/p/9299275.html 1.下载镜像 docker pull mysql:5.7.19 2.建立配用 ...
- win10系统桌面快捷键图标异常解决方法
win10系统桌面快捷键图标异常解决方法 前言: 有一次我的一个图标变成白色,找到:https://jingyan.baidu.com/article/948f5924148e67d80ef5f947 ...
- Spring事务的传播:PROPAGATION_REQUIRED
PROPAGATION_REQUIRED-- 支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. ServiceA { void methodA() { ServiceB.method ...
- 关于mybatis缓存配置讲解
一级缓存: 一级缓存是默认的. 测试:在WEB页面同一个查询执行两次从日志里面看同样的sql查询执行两次. 2次sql查询,看似我们使用了同一个sqlSession,但是实际上因为我们的dao继承了S ...
- 【JavaScript】快速入门
摘抄地址快速入门 No1: JavaScript严格区分大小写 No2: JavaScript不区分整数和浮点数,统一用Number表示 NaN表示Not a Number,当无法计算结果时用NaN表 ...
- HDU 2242 考研路茫茫—空调教室 (边双连通+树形DP)
<题目链接> 题目大意: 给定一个连通图,每个点有点权,现在需要删除一条边,使得整张图分成两个连通块,问你删除这条边后,两联通块点权值和差值最小是多少. 解题分析: 删除一条边,使原连通图 ...
- HDU 1045 Fire Net 【二分图匹配】
<题目链接> 题目大意: 这题意思是给出一张图,图中'X'表示wall,'.'表示空地,可以放置炮台,同一条直线上只能有一个炮台,除非有'X'隔开,问在给出的图中最多能放置多少个炮台. 解 ...