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

Vuex allows you to split your store into different contexts by using modules.

In this lesson we’ll see how can we split the store in multiple Vuex modules using TypeScript

From the code we have before:

import Vue from 'vue';
import Vuex from 'vuex'; import todos, {getters, mutations, actions} from './todos'; Vue.use(Vuex); export default new Vuex.Store({
state: {
...todos,
},
getters,
mutations,
actions
});

To:

import Vue from 'vue';
import Vuex from 'vuex'; import {todos} from './todos'; Vue.use(Vuex); export default new Vuex.Store({
modules: {
todos,
}
});

You can put 'getters, actions, mutations, state' into 'modules' which has many features as key

modules: {
todos: {getters, actions, state, mutations},
login : {getters, actions, state, mutations},
}

Todo store:

import {GetterTree, MutationTree} from 'vuex';
import {State} from '../types';
import {Todo} from '../types'; const state: State = {
todos: [
{text: 'Buy milk', checked: false},
{text: 'Learning', checked: true},
{text: 'Algorithom', checked: false},
],
}; const getters: GetterTree<State, any> = {
todos: state => state.todos.filter(t => !t.checked),
dones: state => state.todos.filter(t => t.checked)
}; const mutations: MutationTree<State> = {
addTodo(state, newTodo) {
const copy = {
...newTodo
};
state.todos.push(copy);
},
toggleTodo(state, todo) {
todo.checked = !todo.checked;
}
}; const actions: ActionTree<tate, any> = {
addTodoAsync(context, id) {
fetch('https://jsonplaceholder.typicode.com/posts/'+id)
.then(data => data.json())
.then(item => {
const todo: Todo = {
checked: false,
text: item.title
} context.commit('addTodo', todo);
})
}
} export const todos = {
actions,
state,
mutations,
getters
};

[Vuex] Split Vuex Store into Modules using TypeScript的更多相关文章

  1. vuex动态引入store modules

    主要解决的问题每次建一个module需要自己去主index.js里面去注册 为了偷懒,也为了避免团队开发时同时对index.js 进行修改引发冲突 所以在index.js中 动态的对子目录和模块进行注 ...

  2. 【vue】vue +element 搭建项目,vuex中的store使用

    概述: 每一个 Vuex 应用的核心就是 store(仓库).“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state).Vuex 和单纯的全局对象有以下两点不同: Vuex 的 ...

  3. 踩坑记录-nuxt引入vuex报错store/index.js should export a method that returns a Vuex instance.

    错误 store/index.js代码如下: import Vue from 'vue'; import Vuex from 'vuex'; import city from './moudle/ci ...

  4. vuex里面的store架构

    将store文件夹分为四个文件夹,分别是actions,getters,mutations,state. action:和mutatation功能是类似的,都是修改state里面的数据,区别是acti ...

  5. Vuex项目实战store

    首先简单了解一下什么是Vuex? Vuex是一个专为Vue.js应用程序开发的状态管理模式.采用集中式存储来管理应用所有组件的状态. 以下是对vuex的使用的简单介绍: 一.安装 npm i vuex ...

  6. 【Vuex】vuex基本介绍与使用

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

  7. [vuex]——使用vuex解决模块间传值问题

    二月的第四个周末,在家.受寒流的影响,深圳天气持续冰冻了好几天,天冷人就变得懒动,迷迷糊糊睡到了快十点,终于在饥饿的催促下起床. 和妹子吃完粥后,百无聊赖.透过窗户,发现太阳依旧没有露头的打算,我们也 ...

  8. vuex : 用vuex控制侧栏点亮状态

    上代码. xxx.vue <template> <div id="xxx"> <div class="layout"> &l ...

  9. [Webpack + React] Import CSS Modules with TypeScript and webpack

    If you try to use CSS Modules in TypeScript the same way you would use them in JavaScript, with webp ...

随机推荐

  1. python--smtp邮件使用

    #构建对象时,第一个是邮件正文,第二个发送类型,plain表示纯文本,最后使用utf-8保证多语言兼容 #如果需要发送html的话,就把plain改为html------>内容使用html构造便 ...

  2. HDU6031 Innumerable Ancestors 倍增 - 题意详细概括 - 算法详解

    去博客园看该题解 题目 查看原题 - HDU6031 Innumerable Ancestors 题目描述 有一棵有n个节点的有根树,根节点为1,其深度为1,现在有m个询问,每次询问给出两个集合A和B ...

  3. SpringMVC-1-(简介及HelloWord)

    首先我们来看一下servlet的处理请求的方式: 一:SpringMVC简介: 一)SpringMVC中的几个重要组件 1.DispatchServlet: 前端控制器,mvc模式中的c,是整个流程的 ...

  4. mycql 多表联合查询

    egon笔记: 1 单表查询 select distinct 字段1,字段2,字段3 from 表 where 约束条件 group by 分组字段 having 过滤条件 order by 排序字段 ...

  5. ESP8266基础篇

    ESP8266基础篇 模块刚到如图所示~2016-01-26 后三张是手机APP的截图,前面是两个ESP8266的硬件模块,当然大家不应定要买两个,自己焊锡一个USB转TTL的串口就行了,有了下面的底 ...

  6. HDFS分布式文件系统的常用命令行操作

    一.HDFS的客户端种类 1.网页形式  =>用于测试 网址为你的namenode节点的ip+50070的端口号,如: 192.168.50.128:50070 2.命令行形式 =>用于测 ...

  7. poj 1386 Play on Words门上的单词【欧拉回路&&并查集】

    题目链接:http://poj.org/problem?id=1386 题目大意:给你若干个字符串,一个单词的尾部和一个单词的头部相同那么这两个单词就可以相连,判断给出的n个单词是否能够一个接着一个全 ...

  8. 数据库相关--mysql8.0设置root登录密码

    系统:win7  64位 MySQL    8.0 .11 1.启动服务: $ net start mysql 进入MySQL(mysql -hlocalhost -uroot): 2.选择mysql ...

  9. javascript宏任务和微任务

    函数 // 你不能改变一个函数的 name 属性的值, 因为该属性是只读的 var object = { // someMethod 属性指向一个匿名函数 someMethod: function() ...

  10. 2018-6-8随笔-combox绑定-语音-删空格

    1.下面介绍三种对comboBox绑定的方式,分别是泛型中IList和Dictionary,还有数据集DataTable ----->>>>>飞机票 2. 简单的语音播报 ...