首页 > 解决方案 > Could anyone make me understand what does the code mean in vue.js

问题描述

Could anyone make me understand the below scenario because I tried searching the web and unable to find any info.

I have the below code which does not work because infox is null. But when i change it "infox: []" then it works fine. I need to understand why is it so ?

data:{
    infox:null
}


 methods: {
      loadmore: function () {
        axios.get(this.url)
this.infox.push(...response.data);
}}

Next I want to understand what does the three dot stands for in ...response.data and why I cannot code in the below manner without three dots which makes more sense. I would really appreciate if you could point me to the source.

methods: {
      loadmore: function () {
        axios.get(this.url)
this.infox.push(response.data);
}}

Below is my JSON data

[
  {
    "Categories": "Fashion", 
    "Clicked": 30, 
    "EndDate": "2019-08-21", 
    "HomepageSpotlight": "No", 
    "ImageMainPage": "/static/images/fashion16.jpg", 
    "MainPage": "Yes", 
    "Mainhomepage": "No", 
    "Rating": 5, 
    "SlugTitle": "buy-clothes-with-50-Off", 

  }, 
  {
    "Categories": "Fashion", 
    "Clicked": 145, 
    "EndDate": "2019-08-21", 
    "HomepageSpotlight": "No", 
    "ImageMainPage": "/static/images/fashion10.jpg", 
    "MainPage": "Yes", 
    "Mainhomepage": "No", 
    "SlugTitle": "get-upto-60-off-on-jeans", 
  }
]

标签: javascript

解决方案


The this.infox variable refers to the infox:null in your example, so it does not work because null, obviously, does not have the method push.

When you change the infox to an Array like infox: [] then it works because an Array does have the method push.

Three dots operator is a new feature in ES6, you can read about it in a lot of articles, for example here: https://dev.to/sagar/three-dots---in-javascript-26ci

In your case the this.infox.push(...response.data) will populate each element of the data into the infox array. Your data is the array it'self, so it will copy the data array to the infox array.

The this.infox.push(response.data) string will result in putting all the data array in just one element of the infox array.


推荐阅读