question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: this.$store does not exist on type class. (Vue 3 + TypeScript)

See original GitHub issue
  • [ x ] I have searched through existing issues
  • [ x ] I have read through docs
  • [ x ] I have read FAQ

Info

  • Platform: Ubuntu 20.04 (WSL2, Ubuntu Budgie)
  • Vetur version: 0.27.3
  • VS Code version: 1.49.1

Problem

Screenshot

Reproducible Case

  1. Use the Vue CLI to create a Vue 3 project with TypeScript using class component style syntax.
  2. Go into the store and create any state object.
  3. In your class (happens in methods, computed, anything), call this.$store,
  4. See vetur complain that this.$store does not exist on the class.

I followed this link: https://discordapp.com/channels/325477692906536972/568037134968160256/756604272484941856 after speaking with some people on the Vue discord, and added the vuex-shim.d.ts file which did fix it but it stopped working again, I swapped it to use Store<any> instead.

See my shim:

import { ComponentCustomProperties } from 'vue'
import { Store } from 'vuex'

declare module '@vue/runtime-core' {
	// Declare your own store states.
	interface State {
		count: number
	}

	interface ComponentCustomProperties {
		$store: Store<any>
	}
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
rodrigoslayertechcommented, May 2, 2021

The same error here using Quasar Framework, after upgrade from Quasar v1 (Vue 2) to Quasar v2 (Vue 3) image image

1reaction
luucaskenjicommented, May 29, 2021

@ngekoding or anyone else looking for a solution to this. I spent hours looking for a solution and the following code worked for me: First, when mounting the app, create a global property for $store:

// main.ts

import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';

const app = createApp(App);
app.config.globalProperties.$store = store;
app.use(router).mount('#app');

then I created this file in src folder:

// shims-vuex.d.ts

import { Store } from 'vuex';

declare module '@vue/runtime-core' {
  interface ComponentCustomProperties {
    $store: Store<T>;
  }
}

I don’t know if it could help, but I stopped and ran the project again (yarn serve) More info here

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue.js 3 and typescript : Property '$store ... - Stack Overflow
$store doesn't seem to be accessible, despite following the Vue 3 instructions. ERROR in src/components/FlashMessages.vue:28:25 TS2339: Property ...
Read more >
‍♂️ ‍♀️ Vue 3 error with using TypeScript: property X does ...
‍♂️ ‍♀️ Vue 3 error with using TypeScript: property X does not exist on type 'EventTarget'. #vue #typescript #showdev #beginners ...
Read more >
Vue.js 3 and typescript : Property '$store' does not exist on ...
Coding example for the question Vue.js 3 and typescript : Property '$store' does not exist on type 'ComponentPublicInstance-Vue.js.
Read more >
$store property not found by TypeScript - Get Help - Vue Forum
TypeScript complains when my computed property function calls this.$store . Property '$store' does not exist on type 'Vue'.
Read more >
How to use Vue 3 with TypeScript - LogRocket Blog
Build an example Vue app in TypeScript with class-based components, ... Vue is flexible, so users are not forced to use TypeScript.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found