[Vuex] Split Vuex Store into Modules using TypeScript
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的更多相关文章
- vuex动态引入store modules
主要解决的问题每次建一个module需要自己去主index.js里面去注册 为了偷懒,也为了避免团队开发时同时对index.js 进行修改引发冲突 所以在index.js中 动态的对子目录和模块进行注 ...
- 【vue】vue +element 搭建项目,vuex中的store使用
概述: 每一个 Vuex 应用的核心就是 store(仓库).“store”基本上就是一个容器,它包含着你的应用中大部分的状态 (state).Vuex 和单纯的全局对象有以下两点不同: Vuex 的 ...
- 踩坑记录-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 ...
- vuex里面的store架构
将store文件夹分为四个文件夹,分别是actions,getters,mutations,state. action:和mutatation功能是类似的,都是修改state里面的数据,区别是acti ...
- Vuex项目实战store
首先简单了解一下什么是Vuex? Vuex是一个专为Vue.js应用程序开发的状态管理模式.采用集中式存储来管理应用所有组件的状态. 以下是对vuex的使用的简单介绍: 一.安装 npm i vuex ...
- 【Vuex】vuex基本介绍与使用
Vuex是什么? 官方解释: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集 ...
- [vuex]——使用vuex解决模块间传值问题
二月的第四个周末,在家.受寒流的影响,深圳天气持续冰冻了好几天,天冷人就变得懒动,迷迷糊糊睡到了快十点,终于在饥饿的催促下起床. 和妹子吃完粥后,百无聊赖.透过窗户,发现太阳依旧没有露头的打算,我们也 ...
- vuex : 用vuex控制侧栏点亮状态
上代码. xxx.vue <template> <div id="xxx"> <div class="layout"> &l ...
- [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 ...
随机推荐
- html5的audio实现高仿微信语音播放效果Demo
HTML部分: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...
- redis 在 php 中的应用
一.redis 在 php 中的应用(Key篇) 二.redis 在 php 中的应用(String篇) 三.redis 在 php 中的应用(Hash篇) 四.redis 在 php 中的应用(Li ...
- Codeforces 781D Axel and Marston in Bitland 矩阵 bitset
原文链接https://www.cnblogs.com/zhouzhendong/p/CF781D.html 题目传送门 - CF781D 题意 有一个 n 个点的图,有 m 条有向边,边有两种类型: ...
- 51Nod1868 彩色树 虚树
原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1868.html 题目传送门 - 51Nod1868 题意 给定一颗 $n$个点的树,每个点一个 $[ ...
- M. Subsequence 南昌邀请赛
链接: https://nanti.jisuanke.com/t/38232 先给出一个s母串 然后给出n个子串 判断是否为母串的子序列 3000ms 2993ms过的.... 蒻鲫的代码: 建立表 ...
- 第一章:python基础语法| 字符编码| 条件语句...
1.编程语言介绍 编程就是写代码,让计算机帮你做事情.计算机底层是电路,只认识二进制0和1.机器语言&汇编语言语言进化历史:机器.汇编.高级.机器语言只接受二进制代码:汇编语言是采用英文缩写的 ...
- Aladdin and the Flying Carpet(唯一分解定理)
题目大意:给两个数a,b,求满足c*d==a且c>=b且d>=b的c,d二元组对数,(c,d)和(d,c)属于同一种情况: 题目分析:根据唯一分解定理,先将a唯一分解,则a的所有正约数的个 ...
- Java大数统计-hdu1316
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1316 题目描述: 给你一个范围,问你在这个范围内有多少斐波拉契数. 代码实现: import java ...
- Binder原理
--摘自<android插件化开发指南> 1.Binder分为Client和Server两个进程: client和server是相对的.谁发消息,谁就是client:谁接收消息,谁就是se ...
- Intellij IDEA实现SpringBoot项目多端口启动
前言 有时候使用springboot项目时遇到这样一种情况,用一个项目需要复制很多遍进行测试,除了端口号不同以外,没有任何不同.这时我们强大的Intellij IDEA就能替我们实现. 实现方法 第一 ...