Property ‘$store’ does not exist on type 'CreateComponentPublicInstance的解决办法
项目:vue3 + Typescript + Vuex
问题原因:Vuex不提供开箱即用的this.$store
属性类型。与TypeScript一起使用时,必须声明自己的模块扩充。
解决办法:在src文件夹下新建文件夹vue.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 {
orbital
}
// provide typings for `this.$store`
interface ComponentCustomProperties {
$store: Store<State>
}
}