javascript - 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",
}
]
解决方案
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.
推荐阅读
- html - CKEditor4 以编程方式检查文本对齐方式
- javascript - 如何将这个球附加到光标上?
- python - Django Admin中用户下拉列表(外键)的排序
- javascript - 如何在 Windows 10 UWP javascript 中检查前台/后台状态
- javascript - 如何从单个数组创建多维数组或对象?
- maven - IntelliJ Maven 和 JavaFX
- vb.net - 在不关闭整个应用程序的情况下无法关闭/卸载插件
- java - 为什么 Formatter(String fileName) 构造函数在不处理 FileNotFoundException 的情况下不起作用?
- amazon-web-services - AWS Amplify AppSync IAM 401
- c# - DocuSign 在 PDF 文档上添加签名字段而不使用 x 和 y 坐标