美文网首页vue
Vue3+TS在CompositionAPI下使用Vuex4

Vue3+TS在CompositionAPI下使用Vuex4

作者: 兰斌Ice | 来源:发表于2021-02-25 14:45 被阅读0次

虽然在Vue3中可以使用Provide/Inject来实现超远距离变量传送,但是只是祖父组件给子组件传送。
如果要全局的进行变量、状态管理,还是要使用到Vuex。
针对Vue3的话版本就要升级到Vuex4。

先进行包的安装。注意针对Vue3的版本,vuex-composition-helpers需要安装next版本

$ npm install vuex@next -S
$ npm install vuex-composition-helpers@next -S

目录结构

└── store
    └── index.ts
└── main.ts

代码

在代码实现方式上,也有很大的不一样,如下所示:

index.ts

import { InjectionKey } from 'vue';
import { ActionTree, createStore, GetterTree, MutationTree, Store, StoreOptions } from 'vuex';

// Declare state struct
declare interface globalStore {
  fakeName: string;
}

// Define All params of StoreOptions
const globalStoreState: globalStore = {
  fakeName: 'Fake Name',
};

const globalStoreGetters: GetterTree<globalStore, any> = {
  fakeName: (state) => state.fakeName,
};

const globalStoreMutations: MutationTree<globalStore> = {
  UPDATE_FAKE_NAME(state, payload) {
    state.fakeName = payload;
  },
};

const globalStoreActions: ActionTree<globalStore, any> = {
  updateFakeName({ commit }, payload) {
    commit('UPDATE_FAKE_NAME', payload);
  },
};

// Define StoreOptions
const globalStoreOption: StoreOptions<globalStore> = {
  state: globalStoreState,
  getters: globalStoreGetters,
  mutations: globalStoreMutations,
  actions: globalStoreActions,
};

// Defind current store key
export const globalStoreKey: InjectionKey<Store<globalStore>> = Symbol();

export default createStore<globalStore>(globalStoreOption);

main.ts

import { createApp } from 'vue';
import App from './App.vue';

import store, { globalStoreKey } from './store';

const app = createApp(App);
// use store
app.use(store, globalStoreKey);

app.mount('#app');

component.vue

<template>
   <p>{{ fakeName }}</p>
</template>
<script lang="ts">
import { defineComponent, inject, reactive, ref } from 'vue';
import { useStore } from 'vuex';
import { globalStoreKey } from '../store';
import { useState, useActions } from 'vuex-composition-helpers';

export default defineComponent({
    setup() {
       // 通过key拿到特定的store
       const store = useStore(globalStoreKey);
       // 这里的 useActions 类似之前vuex的mapActions
       const { updateFakeName } = useActions(store, ['updateFakeName']);

       return  {
           // 这里的 useState 类似之前vuex的 mapGetters
           ...useState(store, ['fakeName']),
       }
    }
})
</script>

相关文章

  • Vue3+TS在CompositionAPI下使用Vuex4

    虽然在Vue3中可以使用Provide/Inject来实现超远距离变量传送,但是只是祖父组件给子组件传送。如果要全...

  • Vue3+Ts在CompositionAPI下使用Mitt

    事件总线在应用开发中是常用的模式。Vue.js在升级到v3之后,相对v2有较大改变,事件总线的实现方式也有所有调整...

  • vue安装veux

    现在使用npm i 会默认安装vuex4,vuex4只适用于vue3,如果使用的vue环境是vue3,安装vuex...

  • pinia与vuex对比

    Vue3 中使用 Vuex的话需要使用Vuex4,并且存在很大缺陷,所以在 Componsition API诞生之...

  • Vuex4+TS+Vue3在CompositionAPI下使用

    虽然在Vue3中可以使用Provide/Inject来实现超远距离变量传送,但是只是祖父组件给子组件传送。如果要全...

  • vue3+typescript项目中读取window对象中属性报

    问题 在vue3+ts开发的项目中,获取挂载在window对象上的属性a报错 解决方法 使用类型断言缺点:在 an...

  • CompositionAPI

    setup函数 created示例被完全初始化之前执行 ref和reactive响应式引用 1.通过proxy对数...

  • vue2 vuex的使用

    vuex在vue2中的使用与在vue3中不同,首先,需要安装vuex3而不是vuex4,然后,需要在全局中进行定义...

  • Failed to parse source for impor

    Vue3 在采用 compositionAPI 时候,Vite编译报错: [vite] Internal serv...

  • vite安装 vuex4和vue-router4

    安装 vuex4和vue-router4 使用 创建文件src/router/index.js创建文件 src/s...

网友评论

    本文标题:Vue3+TS在CompositionAPI下使用Vuex4

    本文链接:https://www.haomeiwen.com/subject/lpoyfltx.html