LoginSignup
0
0

More than 1 year has passed since last update.

TypeScript+VueにVuexを導入した際のインクリメントエラーについて

Last updated at Posted at 2022-08-28

解決したい問題

npmにてVuexをインストールした際、「$Store」が使えない場合があります。
イメージとしてはこんな感じのエラーが発生します。
image.png
エラー内容

Property '$store' does not exist on type 'CreateComponentPublicInstance<{ [x: string & `on${string}`]: ((...args: any[]) => any) | undefined; } | { [x: string & `on${string}`]: undefined; }, {}, { hoge: ResItem[]; isLoading: boolean; }, { storeItems(): any; }, ... 13 more ..., {}>'.

「プロパティ '$store' がタイプに存在しません。」とコンパイルエラーが出ています。

環境
VueCLIにてVue3をインストール後、
Vuex4をインストールした際に本事象が発生しました。

この問題の解決策

型定義ファイルの導入

VuexのTypeScriptサポート章に従って、Store型を導入しましょう。
このページで述べている「vuex.d.ts」を自分のプロジェクトに追加してください。

それでもうまくいかない時
エディタの再起動を試してみてください。
上記で作成した型を認識しないことがあるようです。

補足

型定義ファイルに関しては以下がわかりやすいです。

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0