[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 ...
随机推荐
- php 操作数据库
$datetoday = date('Y-m-d'); $datetime = $thedate; $data_info = $data; $db = array( 'dsn' => 'mysq ...
- Codeforces 1000G Two-Paths 树形动态规划 LCA
原文链接https://www.cnblogs.com/zhouzhendong/p/9246484.html 题目传送门 - Codeforces 1000G Two-Paths 题意 给定一棵有 ...
- Python中type和object
type 所有类是type生成的 a = 1 b = "abc" print("type a:{}".format(type(a))) print(" ...
- forward 和redirect
http://www.cnblogs.com/davidwang456/p/3998013.html
- nginx重启命令
service nginx restart nginx -s re
- POJ 2763 Housewife Wind 【树链剖分】+【线段树】
<题目链接> 题目大意: 给定一棵无向树,这棵树的有边权,这棵树的边的序号完全由输入边的序号决定.给你一个人的起点,进行两次操作: 一:该人从起点走到指定点,问你这段路径的边权总和是多少. ...
- asp.net core选项Options模块的笔记
这篇博客是写给自己看的.已经不止一次看到AddOptions的出现,不管是在.net core源码还是别人的框架里面,都充斥着AddOptions.于是自己大概研究了下,没有深入,因为,我的功力还是不 ...
- 一道有意思的找规律题目 --- CodeForces - 964A
题目连接: https://vjudge.net/problem/1502082/origin 这一题第一眼看过去貌似是模拟,但是根据其范围是1e9可以知道,如果暴力解基本上是不可能的(不排除大佬级优 ...
- 洛谷P3375 [模板]KMP字符串匹配
To 洛谷.3375 KMP字符串匹配 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果 ...
- go channel tips
一.只有一个goroutine时,读写阻塞的chan会出错(“fatal error: all goroutines are asleep - deadlock!”).包括未make的chan(cha ...