vuex使用$store出现类型“{ $: ComponentInternalInstance; $data: {}; $props: { style?: unknow不存在属性“$store“问题。

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.tsstore.index.ts中不要同时声明类似state中的内容容易造成冲突

猜你喜欢

转载自blog.csdn.net/m0_60820509/article/details/132768623