gpt4 book ai didi

javascript - 如何在 vue 3 和 typescript 中正确键入 vuex 模块

转载 作者:行者123 更新时间:2023-12-05 00:37:56 26 4
gpt4 key购买 nike

我想弄清楚如何在 vue 3 typescript 项目中键入 vuex 模块。这方面缺乏官方文档。
假设我有一个这样的项目:

import { createStore, useStore as baseUseStore, Store } from 'vuex';
import { InjectionKey } from 'vue';

interface FruitState {
apple: boolean,
peach: boolean,
plum: boolean
}

const FruitModule = {
namespaced: true,
state: (): FruitState => ({
apple: true,
peach: false,
plum: true
}),
mutations: {},
action: {}
}


export interface State {
foo: string;
}

export const key: InjectionKey<Store<State>> = Symbol();

export const store = createStore<State>({
modules: {
fruit: fruitModule
},
state: {foo: 'foo'},
mutations: {
changeFoo(state: State, payload: string){
state.foo = payload
}
},
actions: {
setFooToBar({commit}){
commit('changeFoo', 'bar')
}}
})

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

...然后在一个组件中:
  const apple = computed(() => store.state.fruit.apple);
当我尝试访问苹果时它不起作用,因为它会引发错误 Property 'fruit' does not exist on type 'State'现在如果我做这样的事情:
import { createStore, useStore as baseUseStore, Store } from 'vuex';
import { InjectionKey } from 'vue';

interface FruitState {
apple: boolean,
peach: boolean,
plum: boolean
}

const FruitModule = {
namespaced: true,
state: (): FruitState => ({
apple: true,
peach: false,
plum: true,
}),
mutations: {},
action: {}
}


export interface State {
foo: string;
fruit?: FruitState;
}

export const key: InjectionKey<Store<State>> = Symbol();

export const store = createStore<State>({
modules: {
fruit: fruitModule
},
state: {foo: 'foo'},
mutations: {
changeFoo(state: State, payload: string){
state.foo = payload
}
},
actions: {
setFooToBar({commit}){
commit('changeFoo', 'bar')
}}
})

export function useStoreTyped() {
return baseUseStore(key);
}
再试一次,错误变为 Object is possibly 'undefined'如果我使用可选链接 ?.,它将允许我访问水果模块
const apple = computed(() => store.state.fruit?.apple);但这对我来说似乎 Not Acceptable ,因为我知道 fruit.apple 实际上不是未定义的。
在 vuex 的状态类型中包含模块的正确方法是什么?

最佳答案

您不需要制作 fruit State 中的可选状态界面:

export interface State {
foo: string;
//fruit?: FruitState;
fruit: FruitState;
}
根据您的评论,您在声明根状态时试图绕过此 TypeScript 错误(如 here 所示):
export const store = createStore<State>({
modules: {
fruit: fruitModule
},
state: { foo: 'foo' }, // ❌ Property 'fruit' is missing in type '{ foo: string; }' but required in type 'State'.
})
使用类型断言作为解决方法:
export const store = createStore<State>({
modules: {
fruit: fruitModule
},
state: { foo: 'foo' } as State, // ✅
})
demo

关于javascript - 如何在 vue 3 和 typescript 中正确键入 vuex 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71187431/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com