ts+vuex,类型“CreateComponentPublicInstance上不存在属性“$store”

最近写vite,封装vuex出现this找不到$store问题。 原因是由于vuex没有像vue-router一样封装$。 解决办法 方法一、给this定

最近写vite,封装vuex出现this找不到$store问题。

原因是由于vuex没有像vue-router一样封装$。

解决办法

方法一、给this定义类型

(this as any).$store.state.xxx

方法二、声明一个$store

import { ComponentCustomProperties } from 'vue'
import { Store } from 'vuex'declare module '@vue/runtime-core' {// declare your own store statesinterface State {}// provide typings for `this.$store`interface ComponentCustomProperties {$store: Store}
}