What are some ways to rejuvenate a computed property?

Is there a way to refresh the data displayed in someData after it has been updated and saved on the server? I have tried using $recomputed('someData'), but it's not working as expected.

I believe I might need to utilize a Promise, but I'm unsure about how and where to implement it. Any assistance with this issue would be greatly appreciated. Thank you in advance for any help provided.

Here is the code snippet of the component:

computed: {
  someData() {
    return this.getData(); // This function retrieves data from the store
  }
},
mounted() {
  this.pullData(); // This function fetches data from the API and commits it to the state 
}

The computed property someData is being used in the template for display, editing, and saving purposes.

And here is the code related to the store:


const actions = {
  async pullData(commit) {
    await ... // Fetch data from APIs and commit it
  }
}

const getters = {
  getData: state.data
}

In addition, a mutation has been implemented.

However, I am facing an issue where the someData is rendered empty once the component is loaded:


data: { 
  someData: [] 
},
mounted() {
  this.pullData();
  this.someData = this.getData();
}

Answer №1

fetchData is a function that has been marked as async, which means it automatically returns a Promise. To access the data returned by getData, you must use the await keyword:

import { mapActions, mapGetters } from 'vuex'

export default {
  async mounted () {
    await this.fetchData()
    this.someData = this.getData
  },
  computed: {
    ...mapGetters(['getData'])
  },
  methods: {
    ...mapActions(['fetchData'])
  }
}

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

Installing Vue CLI on Mac OS may not function correctly

I recently installed vue-cli by executing the following command: npm install -g @vue/cli /Users/me/npm/bin/vue -> /Users/me/npm/lib/node_modules/@vue/cli/bin/vue.js /Users/me/npm/lib └── @vue/<a href="/cdn-cgi/l/email-protection" class="__cf_ ...

Ways to restrict the quantity of Firebase compound indexes required for querying with various filters

I'm in the process of developing a Firestore project that includes group chats and forums where users can filter posts based on various criteria: Number of likes Presence of attachments (photos and/or files) 4 Tags (Questions, Exams, Assignments, Not ...

Having trouble showing information from axios response in a Vue component

The data fetched with axios isn't appearing in the Vue component, even though there is data showing up in vue-dev tools. I'm also encountering errors like: [Vue warn]: Error in render: "TypeError: Cannot read property 'logbook_id' of nu ...

Create vertical boundaries with the chartjs-plugin-annotation in conjunction with vue-chartjs

Trying to set 2 vertical lines as thresholds for a list of results from various tests. The issue arises when attempting to place the lines at values that do not exist within the result set, causing them to disappear. However, with the code below, the lines ...

Save the outcome of the apollo query in a local state?

Imagine having a collection of condensed items associated with user IDs. When clicked, I aim to asynchronously fetch and display additional data using an Apollo query - like username, address, etc. How can I effectively "cache" or store this information lo ...

Is it possible to utilize Vue's splice method within a forEach loop in order to delete multiple elements at

I am currently working on an image grid in array form. I am trying to implement a multi-deletion functionality. When a checkbox for each image in the grid is checked, I store the selected image's index in an array called selectedImages. Upon clickin ...

Designing an architecture with Rails API and VueJS server layout

Currently, I am working on a Rails API only app for the backend, along with two VueJS apps serving as the front ends. These Vuejs apps are making multiple calls to the Rails API. As I contemplate deploying my project to Digital Ocean, one question arises: ...

Do not use a template for keying. Instead, place the key directly on physical elements

I'm trying to incorporate the image preview feature using the image component within the View Design framework, but I keep encountering an error. You can find the Component url here under Example 5. Below is the code snippet: <template> < ...

Download an image file directly from an HTTP response in Laravel using Vue, Axios, and Intervention Image

Currently, I am in the process of setting up a media asset database by utilizing Laravel, Vue, Axios, and Intervention image. One of my main objectives is to allow the user to specify their desired image height and width before proceeding with the download ...

Exploring the Power of Nuxt's asyncData Feature for Handling Multiple Requests

Within my application, there is a seller page showcasing products listed by the specific seller. Utilizing asyncData to retrieve all necessary data for this page has been beneficial in terms of SEO. asyncData ({params, app, error }) { return app.$axi ...

How do I test Pinia by calling one method that in turn calls another method, and checking how many times it has been called

As I embark on my journey with Vue 3 and Pinia, a particular question has been lingering in my mind without a concrete answer thus far. Let's delve into the crux of the matter... Here's an example of the store I am working with: import { ref, co ...

Is it possible to extract the body from the post request using req.body.item?

After working with Express, I learned how to extract body data from a post request. Most examples showed that using req.body.item should retrieve the desired value for tasks like inserting into a table. However, in my case, I found that I couldn't ac ...

In the Show Code feature of Storybook, every element is being displayed for

My Vue 3 + Storybook setup is all running smoothly, except for a little hiccup when I click "Show Code". It seems to display everything instead of just the template. Can anyone help me figure out what's going wrong? https://i.stack.imgur.com/zzBl0.pn ...

What is the best way to customize the look of the v-calendar component in Vue.js?

I've recently started a project that involves Vue.js, and I needed to create a datepicker for it. After some research, I opted to utilize the v-calendar package. The implementation of the component went smoothly and functioned as expected right out o ...

What is the best way to catch an event triggered by an ElementUI tree in Vue.js?

I am utilizing the ElementUI Tree component as shown below: <el-tree :data="data" :props="defaultProps"> </el-tree> As per the documentation, a node-click event is triggered when a node is clicked. How can I se ...

Having trouble adding/removing/toggling an element class within a Vue directive?

A successful demonstration can be found at: https://jsfiddle.net/hxyv40ra However, when attempting to incorporate this code within a Vue directive, the button event triggers and the console indicates that the class is removed, yet there is no visual chang ...

A Vue filtering feature that consistently adds 5 additional elements upon each use

I was wondering, how can I create a computed property filter function that always adds 5 more elements to the current array? Here are more details: Template: <span class="box-content" v-for="item in activeItems" :key="item.id"> <img class=" ...

The transitions in Vue do not seem to be functioning properly when used with router-link and $router

I have the following structure in my App.vue file: <script setup> import { RouterView } from "vue-router"; </script> <template> <RouterView v-slot="{ Component }"> <transition :name="fade" mod ...

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. ...

The content of the string is not appearing

I've hit a snag while trying to create components and templates. I'm closely following the instructions provided in this link: My code is very similar to the one shown in the instructional video (except for the error, of course). The issue I&ap ...