Vue state mapping not updating dynamically

When attempting to retrieve state values from the store using the mapState function, I encountered an issue where the generated code did not properly return the values for use in my template...

<template>
         // Some code
                <template v-if="!isLoggedIn">
                    // Some code
                </template>
                <template v-else>
                    // Some code
                    {{ currentUser.name }} 
                </template>
    
        // Some code
</template>

<script>
    import { mapState } from "vuex";

    export default {
        // Some code
        computed: {
          ...mapState({auth : ['currentUser', 'isLoggedIn','customers']})
        }
    }
</script>

The above code did not work as expected. Instead, I found that the following code provided a solution:

<script>
    import { mapState } from "vuex";

    export default {
        // Some code
        computed: {
          currentUser() {
                return this.$store.state.auth.currentUser
            },
            isLoggedIn() {
                return this.$store.state.auth.isLoggedIn
            },
        }
    }
</script>

A warning message was displayed when using the initial approach:

[Vue warn]: Property or method "isLoggedIn" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://v2.vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

Thank you in advance.

Answer №1

To properly access non-root properties, you can use the correct syntax with arrow functions like this:

computed: {
...mapState({
  currentUsers: state => state.auth.currentUsers,
  loggedIn: state => state.auth.loggedIn,
  clientList: state => state.auth.clientList
})}

For more information, refer to the official documentation.

Answer №2

In order to retrieve data from a vuex module that is namespaced as auth, you must specify the module name as the first parameter and an array of values to map as the second parameter:

computed: {
    ...mapState('auth', ['currentUser', 'isLoggedIn','customers'])
}

Answer №3

To access the module's state, you can use `this.auth.isLoggedin` after mapping it.

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

Error: JSON parsing stopped due to unexpected end of file while attempting to parse data

After testing with other APIs successfully, I found that this particular one is not functioning as expected. const express = require("express"); const https = require("https"); const bodyParser = require("body-parser"); const ...

Error in custom script functionality on Blazor server-side

I recently encountered an issue with my Blazor Server side project that involved integrating the Black Dashboard template from Creative Tim. Initially, my components, specifically the text fields, were functioning correctly with MudBlazor. However, upon ad ...

Why is the Axios instance/function not being passed the argument?

I am currently working on refactoring a next.js App that has a functioning axios call. However, I have encountered an issue with my new function not receiving arguments. The problem consists of two main components: my next.js page and an external custom m ...

Refine an HTML table by a specified value and eliminate columns using JavaScript to display a new table

Hey there, I'm currently working with two HTML tables labeled id="source" and id="destination". The source table contains 7 columns while the destination table only has 5 columns. My goal is to filter data based on col2 (with a value of 150) from th ...

What is the best method for gracefully opening external links in a reusable new tab?

Here is the progress I have made so far: <script> var win; function OpenInNewTab(url ) { // var win; if (win) { win.close(); } win =window.open(url, 'myWin'); win.focus(); } </script> My links are structured like ...

"Customize the centering offset of Google Maps based on the width of

Struggling to implement a design using my code, but it's not behaving as expected. When the old width is larger than the new width, the offset Center should be -50. And when the old width is smaller, the offset Center should be 50. The offset Center ...

"Troubleshooting alert: Encounter an error message saying 'process is not defined' when attempting to load the grpc package using proto-loader and grpc-js within a

Looking for help with integrating a typescript Vue.js component that needs to make a grpc call. The proto file can be found here: https://github.com/floydjones1/ts-node-grpc/blob/main/proto/random.proto Here is the component.vue code snippet: <script ...

Add HTML code into a contenteditable element and then include additional text following the inserted HTML

I'm working with a contenteditable div and a button that inserts a simple span. <button id="insert-span">Insert</button> <div id="edit-box" contenteditable="true"></div> <script> $('#insert-span').on(' ...

SweetAlert: The title argument is not present

I encountered an error when the error function is triggered from sweet alert. How can I resolve this issue? Appreciate any help! SweetAlert: Missing "title" argument! Here is my JavaScript code: function DeletePost() { swal({ title: "Are you ...

Transferring data from a PHP array to a JavaScript array

Trying to transfer values from a PHP array to a JavaScript array and facing an issue. Here's what I'm doing: $k_data = json_encode($k) This results in k_data = [2,3,4,8,9] In JavaScript, my code looks like this: var s4 = [[]]; for(var i = ...

Ionic JavaScript function runs independently of promise resolution

Within my ngOnInit on a page, I have a method that is meant to fetch data. However, it seems like the other methods in the chain are executing before the fetch data process is complete. Here's a snippet of the code: ngOnInit() { this.devicesinfo.fetc ...

Exploring the Art of Navigation with Ionic and Angular

Trying to integrate Angular, Meteorjs, and Ionic Framework has been a smooth process, thanks to the urigo:angular and urigo:ionic packages. However, I'm facing difficulties in configuring ionic links and angular routing to make it work seamlessly. Des ...

JavaScript generic type for the superclass

Is it possible to create an extendable parent class with a generic type in JavaScript, similar to how it's done in Java? public class Parent<T> { private T t; public T get() { return t; } ... If so, what would the child class l ...

Looking for assistance with creating a D3 bar chart that involves selecting a specific year and crime category? Let me help

I am still learning D3 and I'm facing a challenge while trying to create a bar chart. My idea is to first select the YEAR radio button, then choose the CRIMEHEAD option, and finally the bar chart should be displayed. Here's my thought process: I ...

jquery button returned to its default state

Jquery Form Field Generator |S.No|Name|Button1|Button2| When the first button is clicked, the S.No label and name label should become editable like a textbox. It works perfectly, but if I click the second button, the field becomes editable as expected, ...

Innovative guidelines originating from a resource attribute

I am facing a challenge with a repeater for a resource that has an attribute containing angular directives mixed with text. My goal is to display form inputs dynamically based on the object's property. <ul> <li ng-repeat="action in actions ...

Pinia seems to be failing to refresh and display the latest image

My store and state is updating correctly. I'm currently using Ionic along with vue.js composition using Pinia. After making a selection on a previous route to choose a new image, the image gets updated properly in pinia, but it does not reactively ch ...

Received an unexpected argument count of 1 instead of the expected 0 while passing a function as a prop to a child component

I transferred the deleteImgfunc function from the insertFarmDiaryDetail component to the InsertFarmDiarySubPage component, which acts as a child component. DeleteImgfunc is a parameter-receiving function. Despite creating an interface and defining paramet ...

Switching the Visibility of Bootstrap Navbar Collapse when Changing Views using AngularJS

Currently, I am facing an issue with the Bootstrap ui-module and AngularJS integration. The problem arises in the mobile view when a user switches views, as the .navbar-collapse remains open instead of collapsing. Despite being new to Angular, I have start ...

What methods can be used to strategically incorporate gray into both the left and right sides of a page design?

I'm currently working on an asp.net MVC layout page and I'm trying to create a layout where the left side and right side are gray. However, I'm not sure how to achieve this using HTML, CSS, or Bootstrap. I specifically need to know how to ...