The constant issue persists with the NuxtJS store state variables continuously coming back as

I am facing an issue with the NuxtJs store where it keeps returning undefined. Even after looking at multiple similar questions, I haven't been able to find a solution. Here is my code snippet:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
    state(){ 
        return{
            count: 500
        }
    },
    getters: {
        getCount: (state) => {
            state.count
        }
    },
})

I attempted accessing it using this method:

this.$store.getters.getCount

I also tried the following approach:

computed: {
        ...mapGetters(['getCount'])
},
created(){
        console.log("count: "+this['getCount'])
}

Below is the error message: https://i.stack.imgur.com/pfFex.png

Answer №1

With Nuxt, the store is automatically set up for you, eliminating the need to create your own.

To simplify things, you can remove the setup code:

// REMOVE ALL THIS:
//
// import Vue from 'vue'
// import Vuex from 'vuex'
//
// Vue.use(Vuex)
//
// const store = new Vuex.Store({/*...*/})

Instead, create a store/index.js file with the following contents:

export const state = () => ({
  count: 500,
})

export const getters = {
  getCount: state => state.count,
}

Now, you can easily access the store in your components:

export default {
  mounted() {
    console.log('count', this.$store.getters.getCount)
  }
}

Check out the demo!

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

Using the dot operator to load an image

Is it possible to using the dot operator to load an image? Let's talk about this as a sample image URL <img src={{GET_PROFILE_DATA.googleProfileData.fullName}} alt="profile" class="home-screen-profile-image"> Take note of the unusual looking ...

Laravel VueJS Vuetable-2 without user authentication

Initially, I attempted all the solutions provided here and on other websites. What I have experimented with: Vuetable-2 not working with Laravel Passport Unable to retrieve data from using vuetable-2, in Vuejs 2 The Issue: I was working on a project (L ...

The error message says: "Unable to access attributes of an unknown object ( 'ownerDocument' )"

I am currently working on using htlm2canvas in Vue.js to capture a screenshot of a specific div, but I keep encountering the following error: Cannot read properties of undefined (reading 'ownerDocument') Should I be importing something within t ...

Tips for creating a vertical scrollbar within a row component

Is there a way to create a scrollable parent v-row with a child v-row overflowing with content so that I can nest another v-row inside the v-col and ensure it remains within the scroll element? <v-row> <v-col> <v-row> <p ...

How can I correctly implement a pinia store within another pinia store in Vue 3?

Within my todosStore pinia store, I am implementing CRUD operations against the backend. To achieve this functionality, I require a separate pinia store called bannerStore for updating the message and class of the bootstrap alert component in the template. ...

Vuetify: Dynamic v-select options based on user selection

Here's a codepen I created as an example: https://codepen.io/rasenkantenstein/pen/qBdZepM In this code, the user is required to select a country and then choose cities only from that selected country. The data of people is stored in an array behind t ...

After redirection to a new page, the Ionic-vue menu malfunctioned

I have developed a reusable header component named Header, which contains an IonMenu component for consistency across pages. The menu is associated with the IonRouterOutlet as per the documentation, but I am encountering an issue when navigating between pa ...

Quasar framework's autocomplete feature failing to display text in dropdown list

I'm currently utilizing the most recent version of quasar (0.17) and I am attempting to implement the autocomplete functionality. Although I can successfully filter the list and choose a value, the text in the autocomplete list is not being displayed: ...

An ELIFECYCLE error was encountered during the production deployment process, resulting in error number

I am currently struggling with a perplexing issue and urgently need to resolve it, so any guidance would be greatly appreciated. My project is built using Laravel and Vue, with Docker managing the image container. The application is deployed on AWS. The ...

Concealing Rows Once a Specified Threshold of Rows Has Been Modified

My knowledge of Javascript is very basic and I am new to coding. I have created a table that updates when a button is clicked. However, I am looking for a way to limit the number of rows in my table. For example, when I add a sixth data entry to the table, ...

How to Retrieve Information from an Array in VueJS

At the moment, the data being displayed on my page is an array, as shown in the snippet below: https://i.stack.imgur.com/zAvrc.png However, I only want to retrieve or display the project names. This is all I have at the moment: fetch(context,id){ ...

The persistent issue of window.history.pushstate repeatedly pushing the identical value

I need some assistance with my Vue application. I am trying to update the URL when a user clicks on an element: const updateURL = (id: string) => { window.history.pushState({}, '', `email/${id}`); }; The issue I'm facing is th ...

The information transmitted from the Laravel controller is not syncing with the Vue variable as expected

I've been working on a web app with a dashboard using Laravel and Vue. While passing data from the controller to the Vue file, the data is received correctly. However, when I try to set it to a Vue variable, the value does not update in the variable. ...

React to the Vue: Only activate the event if the key is pressed twice consecutively

In my application, I am creating a unique feature where users can trigger a window to appear by inputting the symbol @ (shift + 50). This will allow them to access predefined variables... <textarea @keyup.shift.50="showWindow"></textarea> My ...

What is the method for customizing the background color in a .vue file using Bootstrap?

Can anyone assist me in updating the background color of a div class from grey to white? The code is within a .vue file using Bootstrap and includes an enclosed SVG file. Here's the snippet: <div class="text-left my-3 bg-white"> <button var ...

Developing a dynamic web application using the Django framework along with the Vue.js library and Highcharts for

I am currently working on a data visualization web app using Django, Highcharts, and JQuery. I have recently transitioned from JQuery to Vue JS and I am struggling with fetching JSON data from a specific URL. Below is the code snippet: Template <!doc ...

The custom element is unfamiliar: Have you properly registered the component? Vue.js in Laravel

I encountered this problem: Unknown custom element - did you register the component correctly? I am trying to implement a like system using Vue.js. Can someone please guide me on how to fix this issue? Despite my best efforts, it seems like everything is i ...

Vue js and axios make it easy to upload multiple files at once

I am encountering an issue where I am attempting to upload multiple images using vuejs and axios, but the server side is receiving an empty object. Despite adding multipart/form-data in the header, the object remains empty. submitFiles() { /* In ...

The functionality of Jquery is successfully integrated into a specific function, however it seems to only work for one of the calls. To make the other call work,

After a user makes a selection, I am attempting to reset the 'selected' item. This process calls the Vue function tS() shown below. The issue lies with the first call $('#sel1').prop('selectedIndex',0);, as it only seems to wo ...

Using nodemailer to send an email with a dynamic variable that holds the HTML content

I am attempting to send a variable containing HTML code from a Vue component using the POST method. My technology stack includes TypeScript, Nuxt.js, Node.js, and Vue.js. const order_list = document.querySelector('table') as HTMLInputElement | n ...