typescript - 属性 '$store' 在类型 'ComponentPublicInstance' 上不存在,使用 Vuex 4 和 typescript

标签 typescript vue.js visual-studio-code vuex

我有一个项目,我在 vuex 中使用 typescript 和 vue,我在 VSCode 上遇到此错误:

Property '$store' does not exist on type 'ComponentPublicInstance<{}, {}, {}, { errors(): any; }, { eliminarError(error: string): void; }, EmitsOptions, {}, {}, false, ComponentOptionsBase<{}, {}, {}, { errors(): any; }, { eliminarError(error: string): void; }, ... 4 more ..., {}>>'
我读了 the documentation它说我必须添加一个包含以下内容的 d.ts 文件:
// vuex.d.ts
import { ComponentCustomProperties } from 'vue'
import { Store } from 'vuex'

declare module '@vue/runtime-core' {
  // declare your own store states
  interface State {
    count: number
  }

  // provide typings for `this.$store`
  interface ComponentCustomProperties {
    $store: Store<State>
  }
}
但随后 VSCode 比 'ComponentCustomProperties' is defined but never used 提示,它仍然显示我提到的第一个错误
我该如何解决这个问题?

最佳答案

我在 VSCode 上也遇到过这个错误,已经解决了。

// vuex.d.ts
import { ComponentCustomProperties } from 'vue'
import { Store } from 'vuex'

declare module '@vue/runtime-core' {
  // declare your own store states
  interface State {
    count: number
  }

  // provide typings for `this.$store`
  interface ComponentCustomProperties {
    $store: Store<State>
  }
}

按照官方文档的配置没有生效。重启VSCode后,终于生效了。

关于typescript - 属性 '$store' 在类型 'ComponentPublicInstance' 上不存在,使用 Vuex 4 和 typescript ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65237129/

相关文章:

typescript 对象类型到数组类型

node.js - Axios前端到Golang后端的CORS问题

git - 将 VSCode 设置为我的 Mac M1 的默认 Git 编辑器

java - 尽管在 Visual Studio 代码中链接了 java jar 文件,但仍然遇到链接器错误

html - Angular2 rc5和 Electron 错误-使用以下命令无法解析组件

Typescript 接口(interface)结合对象属性上的映射键

vue.js - 如何从组件外部销毁vuejs组件

javascript - 在 Vue JS 中将数组子项更新为父项的正确方法是什么

python - 如何阻止 vs code 生成 .ipynb 文件?

javascript - TypeScript import/as vs import/require?