vue3 vuex4 中使用ts
vuex
// main.ts
import { createApp } from "vue";
import App from "./App.vue";

import store from "./store";

const app = createApp(App);

useAnt(app);
store.forEach(({ modelName, key }) => {
  app.use(modelName, key);
});
.mount("#app");
// store/index.ts
import appStore, { key as appKey } from "./modules/app";
// import userStore, { key as userKey } from "./modules/user";
const modules = [
  {
    modelName: appStore,
    key: appKey
  },
  // {
  //  modelName: userStore,
 //   key: userKey
 // },
];

export default modules;
// store/modules/app.ts
import { InjectionKey } from "vue";
import { createStore, useStore as baseUseStore, Store } from "vuex";

export interface State {
  now: number;
}
export const key: InjectionKey<Store<State>> = Symbol();

export default createStore<State>({
  state: {
    now: Date.now()
  },
  mutations: {
    setNow(state, data: number) {
      state.now = data;
    }
  },
  actions: {}
});

export function appStore() {
  return baseUseStore(key);
}

参考:

1、TypeScript支持

2、https://github.com/vuejs/vuex/issues/1833#issuecomment-736418224

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇