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. JavaScript 组数去重demo

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Flask---第二个例子--Get和POST发送

    *get:浏览器告诉服务器,我只需要获取页面信息给我,这是最简单最常用的方法 *Post:览器告诉服务器:想在 URL 上 发布 新信息.并且,服务器必须确保 数据已存储且仅存储一次.这是 HTML ...

  3. 033 Url中特殊字符的处理

    在url跳转页面的时候,参数值中的#不见了,一直没有处理,今天有空看了一下,后来发现后台的过滤器之类的都没有处理,就比较奇怪了,原来是特殊字符的问题. 一:Url中的特殊字符 1.说明 这里还是需要做 ...

  4. mac docker环境搭建mysql主从同步服务器

    参考地址:https://www.cnblogs.com/jinjiangongzuoshi/p/9299275.html 1.下载镜像 docker pull mysql:5.7.19 2.建立配用 ...

  5. pageHelper多个sql分页

    之前有个需求,在一个页面中需要有多个sql分页查询然后放到一个list中,展示,但是会出现一个bug,就是每次分页都会展示第一条查出的所有的数据: 第一页 第二页 因为是截的生产环境,第一条数据被处理 ...

  6. 2017Nowcoder Girl D - 打车

    题目描述 妞妞参加完Google Girl Hackathon之后,打车回到了牛家庄. 妞妞需要支付给出租车司机车费s元.妞妞身上一共有n个硬币,第i个硬币价值为p[i]元. 妞妞想选择尽量多的硬币, ...

  7. HBase读写数据的详细流程及ROOT表/META表介绍

    一.HBase读数据流程 1.Client访问Zookeeper,从ZK获取-ROOT-表的位置信息,通过访问-ROOT-表获取.META.表的位置,然后确定数据所在的HRegion位置: 2.Cli ...

  8. 不一样的go语言-error

    前言   go语言的error处理方式,在目前流行的编程语言中属于刺头.似乎天生就是用来有别于他人标记.TIOBE排行榜全十除了C语言,无一例外是try catch的阵营.而排在go之前的语言除了C与 ...

  9. SpringMVC(十五) RequestMapping map模型数据

    控制器中使用map模型数据,传送数据给视图. 控制器参考代码: package com.tiekui.springmvc.handlers; import java.util.Arrays; impo ...

  10. Codeforces.226D.The table(构造)

    题目链接 \(Description\) 给定一个\(n\times m\)的矩阵\(A_{i,j}\),每次可以将一列或一行取负.求一个方案使得若干次操作后,每行每列的和都非负. \(n,m\leq ...