typescript - 如何为 Vuex 定义 VueJS3 Typescript 接口
问题描述
我有一个带有 Typescript 和 Vuex 4 的 VueJS 3 项目。我按照Vuex中的描述创建了商店。
如何在 state.ts 中定义我的接口,以便Array<Athletes>在“state”-Object 中正确实现???
// store.ts
import {InjectionKey} from "vue"
import {createStore, Store, useStore as baseUseStore} from "vuex"
import {State} from "../interfaces/state";
export const key: InjectionKey<Store<State>> = Symbol()
export const store = createStore<State>({
state: {
athletes: [],
},
...
});
export function useStore() {
return baseUseStore(key)
}
// state.ts
// Interface for the store
import {Athlete} from "./athlete"; // Athlete Interface
export interface State {
athletes: Array<Athlete>
}
解决方案
推荐阅读
- css - 你如何处理 BEM 的孙子?
- node.js - 缩小无头木偶截图的大小
- google-chrome-extension - 如何在 chrome 扩展的内容脚本中包含 socket.io 的客户端
- flutter - 如何使用 FutureBuilder 显示图像?
- python - 是否可以在 Django 中创建更新的新闻源
- javascript - Select2 错误无法读取未定义的属性“getAttribute”
- python - 有没有为此的Python函数?
- string - 尝试使用 DataLoader 类将字符串列表转换为整数列表,但它一直说整数列表为空?
- spring - 使用非反应性 ServerResponse 下载 Spring 文件
- python - Django:计数相关模型,其中相关注释具有特定值并将计数存储在注释中(或简单地说:计数子查询)