Error message in Vuex4: Unable to access 'state' property because it is undefined

Having trouble with Vue3 and Vuex4, I keep encountering this error:

Uncaught TypeError: Cannot read properties of undefined (reading 'state')
    at ReactiveEffect.eval [as fn] (App.vue?3dfd:36)
    at ReactiveEffect.run (reactivity.esm-bundler.js?a1e9:160)
    at ComputedRefImpl.get value [as value] (reactivity.esm-bundler.js?a1e9:1087)
    at setup (App.vue?3dfd:37)
    at callWithErrorHandling (runtime-core.esm-bundler.js?5c40:6656)
    at setupStatefulComponent (runtime-core.esm-bundler.js?5c40:6272)
    at setupComponent (runtime-core.esm-bundler.js?5c40:6228)
    at mountComponent (runtime-core.esm-bundler.js?5c40:4081)
    at processComponent (runtime-core.esm-bundler.js?5c40:4056)
    at patch (runtime-core.esm-bundler.js?5c40:3651)

Attempting to assign "hello" to the message variable for use across the application. Here's my main.js:

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


const store = createStore({
  state(){
    return{
      message: 'hello'
    }
  }
})


createApp(App).use(router, store).mount('#app')

In my App.vue file, I'm trying to access it using the composition api with the computed function.

import { ref, computed } from 'vue'
import { useStore } from 'vuex'

export default {
  name: 'App',
  setup(){
    const store = useStore();

    const message = computed(() => store.state.message);
    console.log(message.value);

    return{ }
  }
}

Answer №1

Suggestion:

initializeApp(App)
  .attach(router)
  .register(store)
  .display('#app')

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

The total number of items in the cart is experiencing an issue with updating

For a recording of the issue, click here: While everything works fine locally, once deployed to production (vercel), it stops working. I've tried numerous approaches such as creating a separate state in the cart, using useEffect with totalQuantity in ...

The height of the div element is automatically adjusted to zero

I am dealing with a simple layout where I have a main div that floats to the left. Within this main div, I nest other divs using the clear both style. Below is a simplified version of my setup: <div id="wrapper" class="floatLeft"> <div id="ma ...

The printer is malfunctioning

My webpage has a div that includes various input fields with values assigned using jQuery. I wanted to print the contents of this div, so I found some code online to help me achieve this. However, when I try to print, the values in the input fields end up ...

The element is not defined in the Document Object Model

There are two global properties defined as: htmlContentElement htmlContentContainer These are set in the ngAfterViewInit() method: ngAfterViewInit() { this.htmlContentElement = document.getElementById("messageContent"); this.htmlContentCont ...

Obtaining the desired element from a function without relying on an event

Recently, I've been working on a sidebar with several links <sidebar-link href="/dashboard" icon="HomeIcon" :is-active="isActive()" /> <sidebar-link href="/test" icon="TestIcon" :is-active=&qu ...

Adjust the log level on winston in real-time

Is there a way to update the log level dynamically in winston and have it reflect across multiple files? I am facing an issue where changing the logger level in index.js does not affect readfile.js. How can I resolve this? Below is the code snippet: win ...

Navigate using jquery on a keyboard starting from a designated input text field

I'm currently developing an internal web application and I want to incorporate keyboard navigation into it. After doing some research, it looks like using JavaScript is the most effective way to achieve this. Below is a snippet of what I have come up ...

Structure of directories in NodeJS

I'm diving into the world of web development and NodeJS, embarking on the task of streamlining our app's code and directory layout. Seeking the insights of seasoned developers, I am keen to gather opinions on the file structure pattern I am cons ...

Utilizing Vue.js to dynamically update input fields based on other field values

I'm a beginner with Vue.js and Vuetify.js, and I am encountering some difficulties with the following: <v-select v-model="car.type" :items="types" label="Type" ></v-select> <div v-if="car.type === 'fiat'"> < ...

Identification of input change on any input or select field within the current modal using JavaScript

My modal contains approximately 20 input and select fields that need to be filled out by the user. I want to implement a JavaScript function to quickly check if each field is empty when the user navigates away or makes changes. However, I don't want t ...

Using jQuery AJAX to Populate a Textbox and Dropdown Menu

I am new to using JQuery AJAX, so I am still learning the syntax. Currently, I am retrieving values from a database and populating a dropdown box. What I would like AJAX to do is fill in three other fields with hardcoded information when a selection is mad ...

Tips for successfully installing a package through npm

Looking to set up nest.js Using the guide provided below. https://www.npmjs.com/package/@nestjs/cli Attempted the following command $ npm install -g @nestjs/cli Encountered this error message. bash: /usr/local/bin/npm: No such file or directory Any ...

Problem encountered while revalidating Next.js and Sanity documents through a webhook

I'm currently developing a Next.js 13 project that utilizes Sanity as its CMS and is deployed on Vercel. To ensure that components and pages are revalidated whenever a new testimonial is added, updated, or removed in Sanity, I have configured a webhoo ...

Determining the location of elements in Three.js: A step-by-step guide

As a newcomer to the world of 3D Graphics programming, I have started using Three.js to develop a software application that involves 3D bin packaging visualization. Currently, my code is focused on creating and positioning two elements. var camera, scene, ...

Having trouble with running sudo commands on Hyper in Windows 10?

Working on a Windows 10 system without any VM software, I've installed hyper to utilize node and npm. My laptop has just one account, which is also the local account administrator. Surprisingly, even though I have all the permissions, I am unable to r ...

Unexpected outcomes when trying to sort and paginate React-Table

Experiencing unexpected results with react-table integration for pagination and sorting. Merged examples from the react-table repository. Encountering an issue where table hooks reset the page index on re-render, causing fetchData to be called twice during ...

AngularJS has encountered an issue with a route resolve promise that has not been completely resolved

I'm currently working on a simple task to manage user roles within routes. The goal is straightforward: Verify the role of the logged-in user on each route (using a resolve function to authenticate the user based on token or login credentials) Direc ...

Leveraging Vue 3 Composition API with accessors

I'm currently in the process of refactoring some of my components using the composition API. One component is giving me trouble, specifically with asynchronous state when trying to retrieve data from one of its getters. Initially, the component was u ...

I'm having trouble installing puppeteer

I tried running the command npm i --save-dev puppeteer to set up puppeteer for e2e testing. Unfortunately, an error occurred during installation: C:\Users\Mora\Desktop\JS\Testing>npm i --save-dev puppeteer > <a href="/cd ...

What is the process for inserting text or letters into a checkbox using Material Ui?

I am looking to create circular check boxes with text inside them similar to the image provided. Any help or guidance on achieving this would be greatly appreciated. View the image here ...