[Vuex] Create a Vuex Store using TypeScript

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

原文地址:https://www.cnblogs.com/Answer1215/p/10771689.html

时间: 2024-11-02 17:40:27

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

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

新建文件夹store,store下: action.js const actions = {} export default actions; getter.js const getters = {} export default getters; mutation-types.js export const publicSetEvent = 'publicSetEvent'; mutations.js import {publicSetEvent} from './mutation-types

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

数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); /*不能直接操作state里面的属性,但是可以创建一个副本*/ 对象 错误的写法:let listData= state.playList; // 对象深拷贝,VUEX就报错 正确的写法:let listDate= Object.assign({}, state.playList); /*不能直接操

[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 your own Vue directive to change a component’s colors and type-safe it with TypeScript. We’ll additionally show how you can pass objects to your directi

vue学习笔记(三)——vuex—store配置

可以将不同视图的仓库放到不同的store中. --store index.js foo.js bar.js --views Foo.vue Bar.vue App.vue main.js 1.配置单个store的信息 foo.js export default{ namespaced: true, //具名引用时使用 state: { //state状态管理(通过store.state.name访问) name: 'waite zhou', age: 25 }, getters: { // st

vuex/store使用

示例代码 main.js中添加代码 import store from "./store"    //导入store new Vue({   el: '#app',   router,   store,                                   //添加一个store   components: {App},   template: '<App/>' }) store文件夹中创建index.js 分为四级操作: state; mutations;

vuex创建store并用computed获取数据

vuex中的store是一个状态管理器,用于分发数据.相当于父组件数据传递给子组件. 1.安装vuex npm i vuex --save 2.在src目录中创建store文件夹,里面创建store.js (1)store.js里引入vue和Vuex, import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) (2)创建并导出store对象 export const store = new Vuex.Store({ }) (3)在st

使用vuex报错“__WEBPACK_IMPORTED_MODULE_1_vuex__.a.store is not a constructor”

import Vue from 'vue'; import Vuex from 'vuex'; Vue.use(Vuex); const store = new Vuex.store({ state:{ num:"我是vuex1" } }); export default store; 控制台显示报错 Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_1_vuex__.a.store is not a constructor (是因为尝试将不是

Vuex的this.$store.commit和在Vue项目中引用公共方法

2018年11月22日 20:50:29 Funfction_Zero 阅读数 3230 1.在Vue项目中引用公共方法 作为一个新人小白,在使用vue的过程中,难免会遇到很多的问题,比如某个方法在很多组件中都能用的上,如果在每个组件上都去引用一次的话,会比较麻烦,增加代码量.怎么做比较好呢,话不多说直接看代码把 首先 要在main.js中引入公共js.然后,将方法赋在Vue的原型链上.像图中这样. 然后在需要的组件上去引入这个方法 mouted (){ //调用方法 this.common.l

25、vuex改变store中数据

以登录为例: 1.安装vuex:npm install vuex --save 2.在main.js文件中引入: import store from '@/store/index.js'new Vue({ router, store, render: h => h(App) }).$mount('#app') 3.在src文件目录下新建一个名为store的文件夹,为方便引入并在store文件夹里新建一个index.js,里面的内容如下: import Vue from 'vue' import