Hor*_*her 6 typescript eslint vue.js vuex vuejs3
我正在尝试使用 Vue3 和 typescript 初始化一个项目,但是将 vuex 添加到项目后,它不会\xc2\xb4t 编译。
\n我做了什么:
\nnpm install vuex@next --save\nRun Code Online (Sandbox Code Playgroud)\n因此,在我的简单存储文件中,我有:
\nimport { type InjectionKey } from \'vue\'\nimport { createStore, Store } from \'vuex\'\n\n// define your typings for the store state\nexport interface State {\n count: number\n}\n\n// define injection key\nexport const key: InjectionKey<Store<State>> = Symbol()\n\nexport const store = createStore<State>({\n state: {\n count: 0\n }\n})\nRun Code Online (Sandbox Code Playgroud)\n但如果我运行npm run build,这就是结果:
src/stores/index.ts:2:36 - error TS7016: Could not find a declaration file for module \'vuex\'. \'/home/user/projects/vue_projects/init-test/node_modules/vuex/dist/vuex.mjs\' implicitly has an \'any\' type.\n There are types at \'/home/user/projects/vue_projects/init-test/node_modules/vuex/types/index.d.ts\', but this result could not be resolved when respecting package.json "exports". The \'vuex\' library may need to update its package.json or typings.\n\n2 import { createStore, Store } from \'vuex\'\nRun Code Online (Sandbox Code Playgroud)\n看起来它已经找到了 vuex 类型,但无论如何都无法导入。我该如何解决这个问题?
\n我已经vuex.d.ts在 vuex 文档之后添加了一个 。
import { Store } from \'vuex\'\n\ndeclare module \'@vue/runtime-core\' {\n // declare your own store states\n interface State {\n count: number\n }\n\n // provide typings for `this.$store`\n interface ComponentCustomProperties {\n $store: Store<State>\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
mas*_*a03 15
这似乎是由相对较新版本的 typescript 的更改引起的,该版本不再识别 vuex 类型定义。
将以下代码保存在适当的目录下,名称如vuex.d.ts. 这应该允许 TypeScript 正确识别 vuex 模块。
declare module "vuex" {
export * from "vuex/types/index.d.ts";
export * from "vuex/types/helpers.d.ts";
export * from "vuex/types/logger.d.ts";
export * from "vuex/types/vue.d.ts";
}
Run Code Online (Sandbox Code Playgroud)
我还没有证实这一点,但使用稍旧版本的打字稿可能会解决问题。
| 归档时间: |
|
| 查看次数: |
4799 次 |
| 最近记录: |