json - 使用 VueJS 访问 JSON 子对象
问题描述
我目前有一个从 API 获取数据的 JavaScript 页面。问题是我无法访问service.statusgroup.name
响应中的对象。
我收到此错误:“TypeError:service.statusgroup 为空”。但是当我删除时.name
,我没有收到任何错误。
JSON响应:
{
"id": 2,
"title": "Website",
"description": "Website accessibility",
"status": "0",
"statusgroup": {
"id": 1,
"name": "Services", <-- Can't get this
"created_at": "2021-03-14T17:08:15.204Z",
"updated_at": "2021-03-14T17:40:29.739Z"
},
"created_at": "2021-03-14T15:06:18.524Z",
"updated_at": "2021-03-14T17:42:22.235Z"
}
JavaScript 获取(VueJS):
export default {
name: 'Status',
mounted() {
// Fetch data from API
axios.get('http://localhost:1337/statuspages')
.then((reponse) => {
this.services = reponse.data
})
},
data: function() {
return {
services: null,
}
},
显示响应:
<template v-for="service in services">
<v-list-item :key="service">
{{ service.statusgroup.name }}
</v-list-item>
</template>
解决方案
推荐阅读
- python-3.x - python中的self关键字用法?详细的解释就像你在解释一个孩子一样
- django - 如何在 django 中使用 mysql-connector mysql 适配器??/
- javascript - select2:如何在动态加载ajax内容后刷新选项,而不重置搜索框?
- c# - 使用通配符搜索文件名列表
- python - 单击 folium 中的 GeoJSON 时限制缩放
- php - 如何将单个数组转换为多维并根据值名称进行分组
- ios - 如何在 ForEach 视图中的每个项目之间添加分隔符?
- vim - 映射 TAB 键以在 vim 中切换拆分窗口
- spring - VScode 中的 Spring-FW => 执行器视图
- intellij-idea - Jenkins GDSL:如何以声明性语法识别构建作业?