Utilizing Vue's v-for directive to display computed properties once they have been fully loaded

Utilizing the v-for directive to loop through a computed property, which is dependent on a data attribute initialized as null. I'm planning to load it during the beforeMount lifecycle hook.

Here's a simplified version of the code:

<th v-for="item in computed_list">
{{ item.name }}
</th>

<script>
export default {
  name: 'test',
  data () {
    return {
      whole_list: null
    }
  },
  beforeMount () {
    this.load()
  },

  computed: {
    computed_list: function() {
      if (!this.series) return []
      return this.whole_list.slice(1,3)
    }
  },

  methods: {
    async load () {
      let res = await some_api_call()
      this.whole_list = res['data']
    }
  }


}
</script>

However, rendering the list failed and throws a

TypeError: Cannot read property 'name' of null
.

Being new to Vue.js, I am still learning about its lifecycle. The main goal here is to display a list of data that will be loaded after creating the Vue instance. Unsure if the current approach is correct.

Answer №1

To maintain VueJS state watching functionality, avoid initializing a data item to null as it can break the tracking of changes. It is recommended to initialize it as an empty object or array instead.

Vue observes data changes by recursively traversing the data object and converting properties into reactive getters and setters using Object.defineProperty. If a property is not present during instance creation, Vue won't be able to monitor it.

You don’t need to set every single nested property in your data initially. Initializing a field as an empty object is sufficient, and you can later assign it to a new object with nested structures, as Vue can effectively observe the nested properties of this new object.

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

Encountering an error in a React application with Redux: "Unable to access the 'state' property of undefined"

I'm currently working on a react redux application where I want to add a message to the message array in the reducer's initial state when the add message button is pressed. However, I encountered an error "TypeError: newstate1.user.id.find is not ...

React typescript is handling various promise response types, causing strange behavior in type-checking

I recently started utilizing the and I seem to be encountering a perplexing issue. If further context is needed, please let me know and I will provide it. All the necessary functions and types are mentioned below my explanatory paragraphs. Your assistance ...

What is the best way to secure videos and other static files with authentication in a next.js web application?

My goal is to provide static content, specifically videos, exclusively to authorized visitors. I want to secure routes so that they are only accessible to authenticated users. However, the challenge arises when trying to display a video on a page located i ...

Error Detected: the C# script is not compatible with Javascript and is causing

I am facing an issue where I can successfully send information from the database, but I am unable to load the table on the page. When I check the data received with an alert, it appears to be in JSON format, but it still displays the wrong image on the web ...

Issue connecting to Vue CLI service running on Docker from host environment

My Dockerfile is quite basic, as it installs Vue CLI and exposes port 8080: FROM node RUN yarn global add @vue/cli EXPOSE 8080 In my docker-compose.yml file, I connect the host port to the container's exposed one: version: '3' services ...

Creating a group object based on ID using react-native and JavaScript - a step-by-step guide

I have an array of objects that I need to reorganize so that each object with the same guid is grouped together. For instance, given this array; [ {"guid":"3a03a0a3-ddad-4607-9464-9d139d9989bf","comment":"text&quo ...

Configure environment variables for either grunt or grunt-exec

Attempting to utilize grunt-exec for executing a javascript test runner while passing in a deployed link variable. This is being done by initializing an environment variable grunt.option('link') with the help of exec:setLink. Within my test_runn ...

Utilizing Laravel Web and API Routes for an Application integrated with VueJS on the front end

Our team is in the process of building a multi-page application with VueJS as our frontend JavaScript framework and Element.io as our CSS framework. We are not currently exposing any web services or APIs. The application we are developing is responsive, ...

Consistently receiving the identical result even when the checkbox remains unselected

Displaying a Checkbox Input <input id="idCheckbox" name="check" type="checkbox" value="AllValue" style="width: auto; height: auto; font-weight: bolder;" data-bind="checked: idCheckbox" /> The checkbox input will always have the value "AllValue ...

Adjust the hue of a specific node

Is there a way to customize the color of an individual node in an eChart treemap? I attempted to set a color property for the node, but it didn't seem to work. While I was able to change the label's color, I'm interested in changing the back ...

Generating a JSON Array Made up of JSON Elements Using JQuery

Currently, my goal is to send a JSON Object to the backend. Essentially, I aim to retrieve the input from 2 textfields and a list of all selected checkboxes. Below is an excerpt of my JavaScript code: function insertNewHero() { var selectedAbiliti ...

Having issues with ng-view in AngularJs - it's not functioning properly

I am having trouble with the ng-view directive not working. When I implement it, all I see is the "Page" title. Can someone help me diagnose the issue? https://jsfiddle.net/dsgfdyp1/3/ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/b ...

employ the inverse Euler application

var a = new THREE.Euler( 0, 1, 1.57, 'YXZ' ); var b = new THREE.Vector3( 1, 0, 1 ); var c = b.applyEuler(a); In order to get the value of c using b.applyEuler(a), I am looking for the reverse operation involving applyEuler. Given that the value ...

Clicking on the icon image that features a Background Video will trigger the video to play in a popup window originating from the same location

A background video is currently playing on the site. When the play icon is clicked, the video should start playing in a popup. The video should start playing continuously from the exact moment and position where it is clicked. Check out the example here. ...

Loading a page ahead of time in NuxtJS

I created a NuxtJS static site with some fixed text, an image in PNG format (stored in the local assets folder), and I also integrated the vue-particles plugin. Whenever someone accesses this page, the content loads in the following sequence: The HTML st ...

Direction of Scrolling

Is it possible to have a div move in the opposite direction of mouse scroll, from the top right corner to the bottom left corner? Currently, it is moving from the bottom left to the top right. #block { position: absolute; top: 400px; left: 100px; < ...

"Utilizing AngularJS event system through $broadcast and $on

I have recently started learning AngularJS and I am trying to send a message from one controller to another. I have looked at various examples and my code seems to be similar, but it is not working. Why is $rootScope.$on not working? Can anyone help me wit ...

What is the correct way to align an InputLabel in Material UI?

https://i.stack.imgur.com/Uafr1.png Looking for advice on styling the InputLabel in my code to improve its appearance. Code snippet below: <FormControl fullWidth> <InputLabel >Select EPE</InputLabel> <Select ...

Is there a way to remove the initial number entered on a calculator's display in order to prevent the second number from being added onto the first one?

I am currently in the process of developing a calculator using HTML, CSS, and JavaScript. However, I have encountered an issue with my code. After a user inputs a number and then clicks on an operator, the operator remains highlighted until the user inputs ...

Inserting data with special characters from an Ajax POST request into a database

I am facing an issue with my form that contains text inputs. When I use an ajax event to send the values via POST to my database PHP script, special characters like ' " \ cause a problem. If the string contains only numbers/letters and no special ...