Can Vuex getters be utilized in a Vue component's computed property?

I have various variables in my vuex-store, such as loggedIn. I need to access this variable from the computed section of my component to filter an array based on its value.

Here is how I am trying to implement it in my code:

<td v-for="(item, index) in navLinks" :item="navLink" :key="index">
  <router-link :to="{ name: item.name }" class="router-links" exact>{{ item.text }}</router-link>
</td>

And this snippet shows the relevant part of my script section:

computed: {
  ...mapGetters(['user', 'loggedIn']),
  navLinks: [
    {
      vif: !this.loggedIn,
      name: 'Register',
      text: 'Registrieren',
    },
    {
      vif: this.loggedIn,
      name: 'Logout',
      text: 'Logout',
    },
  ],
},

However, I keep encountering the error:

Uncaught TypeError: Cannot read property 'loggedIn' of undefined

This makes me assume that Vuex getters cannot be used before a component mounts due to this not being defined..?

If I were to use navLinks in the data with v-if-directives, I would receive an error because v-if bindings are incompatible with v-for bindings.

So, how can I resolve this issue?

Answer №1

navLinks is a function that should return an array:

computed: {
  ...mapGetters(['user', 'loggedIn']),
  navLinks(){
    return [
      {
        vif: !this.loggedIn,
        name: 'Register',
        text: 'Registrieren',
      },
      {
        vif: this.loggedIn,
        name: 'Logout',
        text: 'Logout',
      },
    ];
   },
},

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

Is there a way to determine if a value exists within an array of objects?

Is it possible to determine if a specific value is present in an array of objects? I've tried a method, but it always returns false. What would be the most effective way to solve this issue? My approach: var dog_database = [ {"dog_name": "Joey" ...

Update the reference of the 'this' keyword after importing the file

I am currently utilizing react-table to showcase the data. My intention is to house my table columns outside of the react component due to its size and for reusability purposes. I created a table configuration file to contain all of my table configurations ...

Utilizing Parent Method in VueJS Component: A Step-by-Step Guide

I need to access methods of the parent component from within the current one, without using props. Below is the structure in HTML: <div id="el"> <user v-for="user in users" :item="user"></user> </div> And here is the Vue code ...

What is the best way to ensure a checkbox in VueJs is pre-selected when rendering?

UPDATE: My issue has taken a different turn, focusing on another code aspect. As a result, I have posted a new question. Within my Beufy Form Field Component, there is a Boolean Input Checkbox that allows users to select the option "Later," which in turn ...

Ensure you are focusing on elements exclusively contained within the parent table and not any child tables nested within its cells

Looking for some help with a unique situation here. I'm struggling to find the right selector for this task. For reference, you can check out the jsfiddle at this link: http://jsfiddle.net/NZf6r/1/ The scenario is that I have a parent table containin ...

Create a loop to iterate through dates within a specified range using the Fetch API

When I need to get the exchange rate from the bank for a specific interval specified in the input, I follow these steps. The interval is defined as [startdate; enddate]. However, in order to make a successful request to the bank, the selected dates must be ...

Updating the background of a button with Vue JS by utilizing an object upon clicking

If you have three buttons and want to change the background color when clicked, for example, clicking on the red button should turn the background color red. However, there is an important detail here: if you click on one button and then another, the old c ...

Is it achievable to set a tab value for an HTML form element?

I'm wondering if it's possible to set a tab character as the value for an HTML dropdown list. Here is the code I currently have: <select id="delimiter-select" class="form-control form-control-sm csv-select"> <option value ...

The <a href="#divtagid"> link is incapable of triggering the opening of the div tag when called from JavaScript

I need help with displaying the content of a div with the id "hello" in maindiv when clicking on the href "Click Here", but it's not working as expected. Here is the code I have: $(document).ready(function() { var newhtml = '<a href="#he ...

Tips for declaring a data variable in Vue with the help of APIs

I'm currently working on a project where I am integrating movie data from an API (TMDb) using Vue Routers. I have a component named 'MovieList' where I am fetching data from the API. Below is the data structure: data() { return { mov ...

Automatically divide the interface into essential components and additional features

Consider the following interfaces: interface ButtonProps { text: string; } interface DescriptiveButtonProps extends ButtonProps { visible: boolean, description: string; } Now, let's say we want to render a DescriptiveButton that utilize ...

The icons from FontAwesome in Vue do not update when computed

I am seeking a way to dynamically change the header icon based on a conversation property. <a class="navbar-item" :title="$t('header.lock')" @click="makePrivate"> <i class="fas" :class="getLockClass"></i> </a> These ...

Using a variable to store the value of the id attribute in HTML code

How can I dynamically add an ID attribute to an HTML element using a variable declared in JavaScript? Using jQuery var table_row = $('table').find('tr#' + pid); var name = table_row.find('td:nth-child(1)').html(); table_ ...

Can you explain the concept of an environment variable in the context of Node/Express?

This question may seem basic, but I haven't found a clear explanation for it yet. In my experience with Node/Express, I always set the following variable: var port = PROCESS.env.PORT || 9000 I understand that PROCESS.env.PORT is related to environme ...

Node.js is great at hosting HTML files, but struggles when it comes to loading script files within those pages

Recently, I’ve been working on creating a simple login page using a mongoDB database along with Node.js and express. Since I'm still new to Node.js, I'm facing an issue that seems more complicated than it actually is. The main problem I’m en ...

What is the reason that a verb function with parameters is preventing another one of the same route from executing?

Currently, I'm honing my skills in node.js and express.js by creating some arbitrary content. Recently, I encountered an issue where one of my get methods with parameters seemed to block another get method from executing. I'm curious to understan ...

Images failing to load in jQuery Colorbox plugin

I am having an issue with the Color Box jQuery plugin. You can find more information about the plugin here: Here is the HTML code I am using: <center> <div class='images'> <a class="group1" href="http://placehold.it/ ...

What is the best way to conceal a row when a particular field is devoid of content?

Is it possible to hide an entire table row if a field is empty? For example: https://i.sstatic.net/tMW7L.png If a certain field is empty, I want the entire row to be hidden, either using JavaScript or jQuery. I have tried some methods but none of them fu ...

Issue with Webpack: Unable to import Vue from an instance stored in a different folder

Currently, I am in the process of integrating Vue.js into a Django project that is a multi-page application. The main objective is to divide the frontend code across the various apps within the project. Unfortunately, I have encountered an issue where I a ...

The getUserMedia() function fails to work properly when being loaded through an Ajax request

When the script navigator.getUserMedia() is executed through regular browser loading (not ajax), it works perfectly: <script> if(navigator.getUserMedia) { navigator.getUserMedia({audio: true}, startUserMedia, function(e) { __ ...