Ways to access states from a Vuex store within a Vuetify list in VueJs

Here is a snippet from my Vue file:

import store from '@/store'
export default{
    name: 'myList',
    data: () => ({
        show: true,
        listContent: [{
                name: '1',
                icon: 'person',
                value: function () {
                    return store.state.myStore.settings.one
                }
            }, {
                name: '2',
                icon: 'person',
                value: function () {
                    return store.state.myStore.settings.two
                }
            }, {
                name: '3',
                icon: 'person',
                value: function () {
                    return store.state.myStore.settings.three
                }
            }
        ]
    })
}

The issue lies in fetching the 'value' from the 'listContent' array.

 {
    name: '3',
    icon: 'person',
    value: function () {
        return store.state.myStore.settings.three
    }
 }

In my implementation, I assumed I could access the view by using :

this.$store.state.myStore.settings.one

However, 'this' inside the value function refers to the object

{
    name: '3',
    icon: 'person',
    value: function () {
        return store.state.myStore.settings.three
    }
}

Despite this, the code continues to malfunction. I aim to retrieve the store within listContent.

The list, presented in the following manner :

<v-data-table :items="listContent" hide-actions hide-headers>
    <template slot="items" slot-scope="props">    
        <td>{{ props.item.name }}</td>
        <td class="text-xs-right" v-text="props.item.value()">  </td>
    </template>
</v-data-table>

I may have erred in referencing the store or incorrectly structured the template. Thoughts?

Answer №1

Why is it necessary for the value to be a function that returns the state value? You can simply assign it directly to state using

this.$store.state.myStore.settings.one

To make this work, change the data option to a regular function instead of an arrow function so that this continues to represent the vue instance

export default {
  name: "myList",
  data() {
    return {
      show: true,
      listContent: [
        {
          name: "1",
          icon: "person",
          value: this.$store.state.myStore.settings.one
        },
        {
          name: "2",
          icon: "person",
          value: this.$store.state.myStore.settings.two
        },
        {
          name: "3",
          icon: "person",
          value: this.$store.state.myStore.settings.three
        }
      ]
    };
  }
};

Answer №2

Perhaps this solution could be of assistance. It is quite lengthy, but it does the job.

const myData = {
  state: {
    test: "module",
    settings: {
      one: "This is one",
      two: "This is two",
      three: "This is three"
    }
  }
};

const storage = new Vuex.Storage({
  modules: { myData }
});

new Framework({
  el: "#app",
  storage,
  Data() {
    return {
      listItems: [
        {
          name: "1",
          icon: "person",
          dataValue: null
        },
        {
          name: "2",
          icon: "person",
          dataValue: null
        },
        {
          name: "3",
          icon: "person",
          dataValue: null
        }
      ]
    };
  },
  Update:{
    '$storage.state.myData.settings.one':{
        immediate:true,
      handler:function(val){
            this.listItems[0].dataValue = val;
      }
    },
    '$storage.state.myData.settings.two':{
        immediate:true,
      handler:function(val){
            this.listItems[1].dataValue = val;
      }
    },
    '$storage.state.myData.settings.three':{
        immediate:true,
      handler:function(val){
            this.listItems[2].dataValue = val;
      }
    },
  }
});

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

A step-by-step guide to incorporating dependencies in an AngularJS controller

I'm currently working on a MEANJS application and I want to integrate Snoocore into an AngularJS controller. Find more about Snoocore here 'use strict'; angular.module('core').controller('HomeController', ['$scope ...

I am having trouble incorporating .obj files into my Nuxt components

I am working on a Nuxt project that includes a background scene created with Three.js. My next goal is to import an .obj file into this scene, requiring the model to be loaded through the component. Here is the code snippet from my index.vue component: ...

ajaxStart - Display change inconsistency

These are some helpful comments to shorten and improve the readability of the code In my current project, I am using Javascript (Ajax) along with PHP (Laravel). I encountered an issue where I have set up two listeners in my ajax function. One listener is ...

Hold up in paths with identical modules

Utilizing vue router for navigating with transitions and vuex to fetch and store data from a JSON API has been successful. The issue arises on routes that utilize the same component. After a route change, there is a momentary display of the old state of t ...

NextJS not maintaining state for current user in Firebase

I'm working on an app utilizing firebase and nextjs. I've set up a login page, but when I try to retrieve the current user, it returns undefined. This issue began a few days ago while working in react native as well - initially, it was related to ...

What could be causing req.body to consistently come back as an empty object?

I am struggling with req.body always returning an empty object regardless of what I try. I have experimented with: var jsonParser = bodyParser.json(); and then including jsonParser in the function -> app.post('/api/get-last-project',jsonParser ...

What is the maximum character limit for the JQuery validation plugin?

Currently, I am utilizing the JQuery validation plugin in my project. My goal is to set a maxlength for one of the fields. Here's an example of how it can be done by defining rules externally: rules: { Message: { required: false, maxLe ...

Response Headers in Google Cloud Functions

When executing a GCF triggered by an Http Request, I encounter the issue of receiving unnecessary headers along with my custom message. Here is a list of headers that are included: HTTP/1.1 200 OK Server: nginx Content-Type: application/json; charset=utf- ...

What is the best method to utilize a promise to delay the execution of a function until the data is received and stored

Currently, I am facing an issue with my API where the model variable is returning undefined before any data is populated in the return_array. I am unsure of how to implement promises or another method to ensure that the variable waits for data to be fille ...

Updating the scope in Angular when changing the image source using ng-src is not working

A snippet inside my controller looks like this: $scope.onFileSelect = function($files) { for(var i = 0; i < $files.length; i++) { var file = $files[i]; $scope.upload = $upload.upload({ url: '/smart2/api/files/profi ...

Tips on importing anime js after it has been successfully installed through npm

Today, I added anime js as a dependency to my package.json file. The version 3.0.1 is visible in the dependencies list. I used npm to install it. Next step was creating a folder and a JavaScript file in the public directory. I set up a simple event liste ...

Unable to refresh the fullcalendar section following an ajax post click

Currently developing a calendar using fullcalendar. I have created an ajax button that retrieves events from another php page. The first click on the ajax button works fine, displaying a nice month calendar with events. However, my issue arises when I cl ...

The responseText array is encountering parsing issues

Utilizing ajax, I am retrieving a json_encoded array from my PHP parser. The responseText returned is ["4.wav","2.wav","3.wav","6.mp3","1.mp3","5.wav"] If I place this into an array like so: var myArray = ["4.wav","2.wav","3.wav","6.mp3","1.mp3","5.wav" ...

Revolutionary scroll-based navigation fill transition

I'm attempting to achieve an effect where the fill color of the "insticon" SVG changes from black to white based on the scroll position indicated in the jQuery code. I have made the necessary modifications in the if and else statements, but unlike "he ...

"Is there a way to retrieve the CSS of all elements on a webpage by providing a URL using

Currently, I am in the process of creating a script that can crawl through all links provided with a site's URL and verify if the font used on each page is helvetica. Below is the code snippet I have put together (partially obtained online). var requ ...

Multer is not recognizing the uploaded file and is returning req.file

This question has definitely been asked multiple times in the past, and I have attempted to implement various solutions without much success. Struggling to upload a file and read its size through Node has left me frustrated. Initially, I tried using the f ...

How can I incorporate the "onClick()" function within an ajax call, while still utilizing the data returned in the success message?

After successfully making an Ajax call, I would like to implement an on-click function while still utilizing the original data retrieved. $.ajax({ URL: ......, type: 'GET', success: function (res) { var Ob ...

Guide to generating an array entry for every line of a text file in node.js

Struggling with converting each line of a text file into an array entry in node.js The array I am working with is named "temp." The code below successfully prints out each line: var temp = []; const readline = require('readline'); const fs = re ...

JavaScript code to make titles slide in as the user scrolls

Looking for a better way to make all titles slide in upon scrolling instead of coding individually? Consider using a forEach loop! Here's how you can modify the code below: function slideInLeft() { document.querySelectorAll('.subtitle-left ...

Consistently directing to a single page on the Node Js web server

I'm in the process of creating a website with multiple HTML pages. Currently, I have code that successfully links to the login page, but unfortunately, the localstores page also directs to the login page. const express = require('express') ...