javascript - Vue 大喊对象未定义,同时它在需要加载它的范围内
问题描述
我想做类似的事情:
- [] 中的 foreach 行
- 解析(行)
- 返回包含 id、类名等的 div...
- 显示返回
但由于某种原因,我收到错误
[Vue 警告]:渲染错误:“TypeError: this.json[index] is undefined”
如果 bool dataIsLoaded 应该确保它被加载,那怎么可能呢?
<div id="app" v-if="dataIsLoaded">
<div v-for="(obj, index) in json.data">
{{ parse(index) }}
</div>
</div>
el: '#app',
data: {
json: null,
dataIsLoaded: false
},
beforeCreate: function () {
fetch("api")
.then(r => r.json())
.then(json => {
this.json = json;
this.dataIsLoaded = true;
});
},
methods:
{
parse: function(index)
{
var type = this.json[index].name;
var body = this.json[index].age;
(...)
return ...
}
}
同时这样的事情正常工作
<div id="app" v-if="dataIsLoaded">
<div v-for="(obj, index) in json.data">
{{ index }}{{ obj }}
</div>
</div>
此外:
parse: function(index)
{
console.log(index);
var type = this.json[index].name;
var body = this.json[index].age;
(...)
return ...
}
console.log(index) 返回例如 0,因此 index 具有适当的值。
解决方案
推荐阅读
- reactjs - React - onclick 后调用 API
- azure-data-factory - 使用 ADF 遍历 blob 存储容器中的所有容器
- r - 将数据框的子集格式化为百分比并在 flexdashboard 中显示
- javascript - 动态导入的组件被视为 JSX 元素
- cmd - 经典的 asp VB Wscript 输出
- c# - 当我通过 EF Core 更新数据库时创建唯一索引重复键
- node.js - Node js中的Docker“清单列表条目中的windows / amd64 10.0.17134没有匹配的清单”
- flutter - 我的 Flutterproject 有问题,无法构建,这是我遇到的错误:
- django - Django不生成primary_key
- mysql - 收到值问题,空对象 Axios/Post/bdd ReactJS