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. Nginx报502错误,PHP最大执行时间设置

    PHP执行时间太长导致的 我在程序的最上方写了set_time_limit(0);不管用 因为max_execution_time在 php-cgi(php-fpm) 中,该参数不会起效. 真正能够控 ...

  2. 分解数据表(将一个datatable按数据量分隔成多个table)

    /// <summary> /// 分解数据表 /// </summary> /// <param name="originalTab">需要分 ...

  3. mysql 备份 恢复

    mysqldump -h127.0.0.1 -uroot -p123456 --databases dbname > e:/mysqlbak/dbname.dump不用新建数据库mysql -h ...

  4. P1441 砝码称重 DFS回溯+DP

    题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 请注意,砝码只能放在其中一边. 输入输出格式 输入格式: 输入文件weig ...

  5. IDEA学习——模板及其常用模板

    模板及其常用模板 (1)psvm (2)sout sout / soutp / soutv / 变量.sout (3)fori iter增强for循环 itar普通for循环 (4)list.for ...

  6. 065 updateStateByKey的函数API

    一:使用场景 1.应用场景 数据的累加 一段时间内的数据的累加 2.说明 每个批次都输出自己批次的数据, 这个时候,可以使用这个API,使得他们之间产生联系. 3.说明2 在累加器的时候,起到的效果和 ...

  7. 简单的使用Nginx框架搭建Web服务器~

    系统环境Debian 8,内核版本 一.首先来安装nginx服务程序:  1.安装nginx服务需要的相关程序(记得在root权限下操作下面的指令) aptitude install libpcre3 ...

  8. 问题 L: An Invisible Hand - (2018年第二阶段个人训练赛第三场)

    题目描述 There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant ...

  9. SQL-内连接、外连接(左、右)、交叉连接

    本文测试基于以下两个表,student(左) \ teacher(右),使用数据库MariaDB,图形化界面HeidiSQL. 连接查询的概念:根据两个表或多个表的列之间的关系,从这些表中查询数据,即 ...

  10. 004.etcd集群部署-动态发现

    一 etcd发现简介 1.1 需求背景 在实际环境中,集群成员的ip可能不会提前知道.如使用dhcp自动获取的情况,在这些情况下,使用自动发现来引导etcdetcd集群,而不是指定静态配置,这个过程被 ...