在vue3+ts+vuex项目中使用$store出现以下:
类型“{ $: ComponentInternalInstance; $data: {}; $props: { style?: unknown; class?: unknown; key?: string | number | symbol | undefined; ref?: VNodeRef | undefined; ref_for?: boolean | undefined; ... 6 more ...; onVnodeUnmounted?: VNodeMountHook | ... 1 more ... | undefined; }; ... 10 more ...; $watch<T extends string | (...”上不存在属性“$store”。
解决方法:
在src文件下添加一个名为 vuex.d.ts 的文件
// vuex.d.ts
import { ComponentCustomProperties } from 'vue'
import { Store } from 'vuex'
declare module '@vue/runtime-core' {
// provide typings for `this.$store`
interface ComponentCustomProperties {
$store: Store<State>
}
}
另外,在vuex.dx.ts和store.index.ts中不要同时声明类似state中的内容容易造成冲突