Encountering an error when trying to access this.$store.state in a basic Vuex

Encountered an error with return this.$store.state.counter:

The property '$store' does not exist on the type 'CreateComponentPublicInstance<{}, {}, {}, { counter(): any; }, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, {}, {}, ... 7 more ..., {}>'.Vetur(2339)

The property '$store' does not exist on the type 'CreateComponentPublicInstance<{}, {}, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, {}, {}, false, OptionTypesType<{}, ... 4 more ..., {}>, ... 5 more ..., {}>'.Vetur(2339)


This Vue project is freshly created using the CLI configuration:

  • Vue version 3.x (Preview)
  • No usage of class-style component syntax
  • Yes to Babel and TypeScript integration
  • ESLint + Prettier are set up as linter and formatter
  • Linting happens on save
  • Configuration files are in separate dedicated files

All files are the default ones generated through Vue CLI with the default project structure.

index.ts file:

import { createStore } from "vuex";

export default createStore({
  state: {
    counter: 8,
  },
  mutations: {},
  actions: {},
  modules: {},
});

App.vue file:

<template>
  <h2>{{ counter }}</h2>
</template>

<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  name: "App",
  components: {},
  computed: {
    counter() {
      return this.$store.state.counter;
    },
  },
});
</script>

main.ts file:

import { createApp } from "vue";
import App from "./App.vue";
import store from "./store";

createApp(App).use(store).mount("#app");


I'm currently learning Vue and trying to access a Vuex store state property for a simple counter example.

I can retrieve the counter state directly in the template using interpolation like {{ $store.state.counter }}, but I face issues when trying to utilize a computed property that returns a value as I'm unable to access the $store object with the keyword this.

return this.$store.state.counter doesn't seem to work


My development environment consists of:

  • VSCode, latest version
  • NPM and Vue CLI
  • Vetur
  • ESLint
  • Prettier


I've attempted similar examples in other project setups, such as those without TypeScript or utilizing Class-Style component syntax. However, Vetur encounters issues in the former case (uncertain if it would cause an error), and while it works in the latter case, I find the syntax unfamiliar and overwhelming to learn due to my lack of experience.


Edit: There are related questions, but none specifically address a fresh Vue CLI project.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to iterate through properties declared in an Interface in Angular 12?

Before Angular 12, this functioned properly: export interface Content { categories: string[] concepts: Topic[] formulas: Topic[] guides: Topic[] } //this.content is of type Content ['formulas', 'concepts'].forEach(c =&g ...

To subscribe to the display of [Object Object], you cannot use an *ngIf statement without being inside an *ngFor loop

In my angular Quiz project, I have a functionality where every user can create quizzes. I want to display all the quizzes that a logged-in user has completed. Here is how I attempted to achieve this: // Retrieving user ID and category ID inside Re ...

Attempt running the Vuetify documentation on your local machine

I have been trying to access the Vuetify docs on my local Mac. Following the instructions in this post: cd /tmp/ git clone https://github.com/vuetifyjs/vuetify.git cd vuetify/packages/docs yarn # option 1 - build and serve yarn build yarn start # optio ...

Tips for fixing the error message "unable to access property 'property-name' of null"

I need assistance with retrieving data from a firebase database and storing it in an array using typescript. Below is the code snippet I am working with: export class ViewUserPage { public list = []; public ref = firebase.database().ref(); public ...

Potentially null object is present in a callback

The code I have is as follows: let ctx = ref.current.getContext("2d"); if(ctx){ ctx.lineWidth=1; // this line executes without errors ctx.strokeStyle=props.barStroke??"darkgray";// this line execut ...

I'm encountering an issue in my node application where it is unable to access the push

I am a beginner in the world of node.js and express. Whenever I try to start my application using the command npm start, I encounter an error message saying Cannot Read property push of undefined from my index.js file. The problematic code snippet looks l ...

What is the best way to utilize a tsconfig "alias path" to import an @ngModule along with other definitions?

Repository Link: https://github.com/andreElrico/mono-repo-test Stackblitz Example: https://stackblitz.com/github/andreElrico/mono-repo-test (noop; only to navigate smoothly) Assume the structure below: root/ ├── projects/ │ ├── app1 │ ...

What is the best way to combine TypeScript output while maintaining node import integrity?

Currently, I am combining the results of the typescript compiler using this particular technique. However, this process is causing issues with the imports of relative path modules in Node. The code below compiles and merges successfully; // Group.ts clas ...

I am having trouble establishing a connection between two containers on Heroku

My web application is built using Node.js and MongoDB. After containerizing it with Docker, everything worked fine locally. However, when I tried to deploy it to production, I encountered an issue where the backend could not establish a connection with the ...

Utilizing TypeORM in a Node.js Project

Recently, I was exploring different ORM options for my server application and came across TypeORM. I'm curious to know the best approach to organize a small project using it. While browsing through the official documentation, I found a repository that ...

Obtaining gender information by utilizing checkboxes in Angular 7

I have developed an Angular application that enables users to filter samples by gender using checkboxes. The options include male, female, or both genders selected. Currently, the filtering works for selecting either male or female individually, as well as ...

What steps do I need to take to integrate the Firebase Admin SDK into my Angular project?

Is there a way to integrate Firebase Admin SDK into my Angular application? Currently, I am using Firebase Authentication services in my application and everything I need for user registration and authentication is handled by Angularfire2. I've read ...

Vue Router failing to render template

I am working on implementing a Vue Router in my Vue application. However, when I click the link, it simply reloads the page instead of displaying the Dashboard template. Any suggestions on how to resolve this issue? Here is the relevant code snippet from ...

Unable to retrieve nested objects from HTTP Response

After receiving data from a HTTP Response, I am trying to access and display it in my template. However, despite storing the data into a component variable, I am encountering issues when trying to access specific properties of the object. { "files": [ ], ...

A guide to implementing previousState in React hooks with TypeScript

I am trying to grasp the concept of using previous state with react hooks in typescript. The code snippet provided below does function, however, it throws an error stating: Type 'number' is not assignable to type 'HTMLDivElement'. Whi ...

Encountering issues following the integration of @angular/flex-layout into an Angular project

After careful consideration, I opted to utilize the responsive grid system provided by @angular/flex-layout instead of Bootstrap. By simply installing the npm package and adding it to my AppModule, I was able to integrate it seamlessly: import { NgModule ...

Utilizing Async/Await with Node.js for Seamless MySQL Integration

I have encountered two main issues. Implementing Async/Await in database queries Handling environment variables in pool configurations I am currently using TypeScript with Node.js and Express, and I have installed promise-mysql. However, I am open to usi ...

Sanity.io's selection of schema field types for efficient and convenient

Hey there, guys! I recently started using Sanity.io and I'm curious whether there's a way to enhance my code efficiency and reuse certain fields across different schemas. I had an idea that goes something like this: cars.ts: export default { ...

Using T and null for useRef in React is now supported through method overloading

The React type definition for useRef includes function overloading for both T|null and T: function useRef<T>(initialValue: T): MutableRefObject<T>; // convenience overload for refs given as a ref prop as they typically start with a null ...

Utilize vue.js and spring-boot to seamlessly upload files utilizing axios

When working with a form that includes a text field and a file upload option, I encountered an issue. The file is named start.vue(say) and it triggers the module.js(say) file. This, in turn, calls service.js(say), which then executes the API in moveControl ...