A Vuex store centralizes the state of your app, making it easy to reason about your state flow.

In this lesson we’ll see how we can create a Vuex store using TypeScript and use it on you class-based component by using the @State decorator from Vuex Class

Install:

npm i vuex vuex-class --save

Create store folder and index.ts, todos.ts file:

//store/index.ts

import Vue from 'vue';
import Vuex from 'vuex'; import todos from './todos'; Vue.use(Vuex); export default new Vuex.Store({
state: {
...todos,
},
mutations: { },
actions: { },
}); // store/todos.ts
import {State} from '../types'; const state: State = {
todos: [
{text: 'Buy milk'},
],
}; export default state;

Define the State interface:

// types.ts

export interface Todo {
text: string;
} export interface State {
todos: Todo[];
}

Using Store in main.ts:

import './hooks';

import Vue from 'vue';
import App from './App.vue';
import router from '@/router';
import store from '@/store/index';
import '@/registerServiceWorker';
Vue.config.productionTip = false; new Vue({
router,
store,
render: (h) => h(App),
}).$mount('#app');

Create a Todos.vue component:

<template>
<ul>
<li v-for="todo in todos">{{ todo.text }}</li>
</ul>
</template> <script lang="ts">
import {Component, Vue} from 'vue-property-decorator';
import {State} from 'vuex-class';
import {Todo} from '../types'; @Component({
})
export default class Todos extends Vue {
@State todos: Todo[]
}
</script>

See the commit

[Vuex] Create a Vuex Store using TypeScript的更多相关文章

  1. vuex中的this.$store.commit

    Vue的项目中,如果项目简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 进行传递 但是如果是大中型项目中,很多时候都需要在不相关的平行组件之间传递数据,并且很多数据需要多 ...

  2. vuex教程,vuex使用介绍案例

    1.demopageaction: import Vue from "vue"; import Store from "../../store.js"; imp ...

  3. 手摸手教你在vue-cli里面使用vuex,以及vuex简介

    写在前面: 这篇文章是在vue-cli里面使用vuex的一个极简demo,附带一些vuex的简单介绍.有需要的朋友可以做一下参考,喜欢的可以点波赞,或者关注一下,希望可以帮到大家. 本文首发于我的个人 ...

  4. vuex基础(vuex基本结构与调用)

    import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const modulesA = { state:{//状态 count: ...

  5. Vuex 页面刷新后store保存的数据会丢失 取cookie值

    在store.js中 export default new vuex.Store({ // 首先声明一个状态 state state:{ pcid: '', postList: [], } //更新状 ...

  6. vuex应用实例-this.$store.commit()触发

    新建文件夹store,store下: action.js const actions = {} export default actions; getter.js const getters = {} ...

  7. VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers

    数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...

  8. vuex介绍和vuex数据传输流程

    1.什么是vuex? 公共状态管理:解决多个非父子组件传值麻烦的问题:简单说就是多个页面都能用Vuex中store公共的数据 a.并不是所有的数据都要放在Vuex中,只有各个组件公用的一些数据会放在V ...

  9. [Vue + TS] Create Type-Safe Vue Directives in TypeScript

    Directives allow us to apply DOM manipulations as side effects. We’ll show you how you can create yo ...

随机推荐

  1. c# 调apicontroller

    public string Post(ModelCs mod) { string formData = Newtonsoft.Json.JsonConvert.SerializeObject(mod) ...

  2. linux系统虚拟机下安装nginx基础

    虽然安装nginx什么的 .以及如何配置等等一系列的资料案例已经很多了 但是作为菜鸟的我还是搞了半天哈 官网上面也有.但是一些细节方面的并没有说明.导致踩了半天坑才搞好 本案例的系统环境     wi ...

  3. python自带进程池

    注意:必须加 close() 和 join(),且 close 必须在 join 之前 代码: import multiprocessing #执行方法 def func(*args,**kwargs ...

  4. 关于thinkphp3自动完成的笔记

    当我在前台传入的主键id与字段表的主键id值时,在更新时tp总是判断为新增的状态(解决办法:将前台的表单主键名保持和数据表主键id名一只,手动创建数据) create时是先获取主键id判断'$type ...

  5. es6的分析总结

    1,var let const对比 1,箭头函数的总结 /** * 1,箭头函数没有this,箭头函数this没有被箭头的函数,所以不能使用call,apply,bind改变this指向 * 2,箭头 ...

  6. java中path和CLASSPATH的配置和意义解析

    原文链接 https://blog.csdn.net/eclipse_yin/article/details/51447169 一.JDK的安装和基本配置 JDK的安装: 1) 如果想要获得JDK,那 ...

  7. 躲不掉的 lambda 表达式

    lambda 表达式是 Java8 的新特性,虽说都发布很久了,但是不到万不得已是肯定不会研究这个的,现在就是那不得不学习的时候了. 本文主要说一下什么 lambda 表达式.Java 中为什么要有 ...

  8. sql关于对一个字段同时满足多条件判断来筛选查询

    表所有数据 查询userName为abc或xyz的 以下为本菜鸟项目中遇到的问题: 背景: /**  * wangjie 180629  *   * 学生需要查询四种可能的消息  * 1.班级管理员发 ...

  9. RabbitMQ主题模式

    Send类 package topics; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; imp ...

  10. Shell脚本笔记(四)条件判断

    条件判断 一.条件测试与比较 一)条件测试基础语法 下列测试语法中如果测试的表达式成立,条件测试就会结束,并返回0. 1.test条件测试 语法:test  <测试表达式> 2.中括号条件 ...