[英]Blank screen in vue.js if not using created() life cycle hook or spread out operator (...)
The following example gives me a blank screen!下面的例子给了我一个空白屏幕! ( jsfiddle here ) (这里是jsfiddle )
HTML: HTML:
<div id="app">
<button @click="objectFromApi">
run objectFromApi function
</button>
<div
v-for="obj in myObject[0].results"
:key="obj.id"
>
<p>
{{ obj.message }}
</p>
</div>
</div>
JavaScript: JavaScript:
new Vue({
el: "#app",
data: {
myObject: []
},
methods: {
objectFromApi: function(){
this.myObject.push(
{
"count": 5,
"results": [
{
"id": 1,
"message": "object 1"
},
{
"id": 2,
"message": "object 2"
}
]
}
)
}
},
//created() {
// this.objectFromApi()
//}
})
Nevertheless it does work if:尽管如此,它确实有效,如果:
1.) Either using objectFromApi
function directly in the created
life cycle hook (what I don't want!) 1.) 要么直接在created
生命周期钩子中使用objectFromApi
函数(我不想要的!)
created() {
this.objectFromApi()
}
2.) Or (without the use of created
life cycle hook) if I go directly into the nested results
array and spread the objects out like this (what I also don't want!) 2.) 或者(不使用created
生命周期钩子)如果我直接进入嵌套的results
数组并像这样展开对象(我也不想要!)
this.myObject.push(
...{
"count": 5,
"next": "http://127.0.0.1:8000/api/someurl/?page=2",
"previous": null,
"results": [
{
"id": 1,
"message": "object 1"
},
{
"id": 2,
"message": "object 2"
}
]
}.results
)
When using method 2.) of course the v-for
loop has to look different:当使用方法 2.) 时,当然v-for
循环必须看起来不同:
v-for="obj in myObject"
instead of v-for="obj in myObject[0].results"
v-for="obj in myObject"
而不是v-for="obj in myObject[0].results"
What is wrong with my initial example?我最初的例子有什么问题?
When the component is first rendering the array myObject
will be empty.当组件首次呈现时,数组myObject
将为空。
During rendering it attempts this:在渲染过程中,它会尝试:
<div
v-for="obj in myObject[0].results"
:key="obj.id"
>
The value of myObject[0]
will be undefined
. myObject[0]
的值将是undefined
。 Attempting to access the results
property of undefined
will result in an error.尝试访问undefined
的results
属性将导致错误。 This error will cause rendering to fail.此错误将导致渲染失败。 Nothing will be shown, even the parts that didn't fail.不会显示任何内容,即使是没有失败的部分。
There are various ways to fix this problem.有多种方法可以解决此问题。 You could prepopulate the data with suitable empty properties:您可以使用合适的空属性预填充数据:
data: {
myObject: [
{
results: []
}
]
}
Alternatively, as you've noted, you could change the loop to use v-for="obj in myObject"
, changing objectFromApi
accordingly to only store the results
array in myObject
.或者,正如您所指出的,您可以更改循环以使用v-for="obj in myObject"
,相应地更改objectFromApi
以仅将results
数组存储在myObject
。 Even if you don't want that exact change some similar change is probably a good idea because the [0]
part strongly suggests you've got a problem with your data model.即使您不想要那个确切的更改,一些类似的更改也可能是一个好主意,因为[0]
部分强烈表明您的数据模型存在问题。 The key thing here is that it avoids trying to access nested objects that don't exist.这里的关键是它避免尝试访问不存在的嵌套对象。 The use of the spread operator in your second example is largely irrelevant.在您的第二个示例中使用扩展运算符在很大程度上无关紧要。
Or you could skip the loop in the template:或者您可以跳过模板中的循环:
<template v-if="myObject[0]">
<div
v-for="obj in myObject[0].results"
:key="obj.id"
>
...
</div>
</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.