vue.js - 未捕获的类型错误:无法读取未定义的属性“国家”
问题描述
<template>
<div>
<Search v-on:onSearch="onWeather"></Search>
<div class="locationBox">
<h1>{{weatherData.sys.country}}</h1>
<p>{{weatherData.name}}</p>
</div>
<div class="weatherBox">
<p>{{weatherData.weather.main}}℃</p>
<p>{{weatherData.main.temp}}</p>
</div>
</div>
和脚本
onWeather(inputs) {
alert(inputs);
let fetchUrl = `${this.baseUrl}weather?q=${inputs}&units=metric&APPID=${this.apiKey}`;
fetch(fetchUrl)
.then((res) => {
console.log(res);
return res.json();
})
.catch((res) => {
console.log(error);
})
.then((result) => {
console.log(result)
return this.onResult(result)
})
},
onResult (results) {
this.weatherData = results;
},
根据响应值写得很清楚,和例子一样,但是出现了标题这样的错误。我很好奇问题是什么。如果您能提供帮助,我将不胜感激。
解决方案
您正在访问一个嵌套对象值,因此您将需要这样的东西:
<h1>{{weatherData.sys && weatherData.sys.country}}</h1>
为什么会出现错误,您可以在此处阅读。
推荐阅读
- python - 有没有办法从 Google Vault Export 下载导出的数据?
- c# - 在 ASP.NET 中使用 window.location 更改位置时会话丢失
- sql - XML 输出在文件中被截断,但在 SSMS 中不被截断
- javascript - Javascript函数未打开模式
- linux - bash: git pull 仅当它是最后一个命令时才有效
- tensorflow - 后续的 dropout 层都连接到 keras 中的第一个层?
- batch-file - 查询 SHA1 / SHA2 / SHA256 的证书
- azure-storage - 使用 Terraform 添加存储虚拟网络规则
- ios - 如何将keyboardAppearance 和inputView?.backgroundColor 同时设置为暗?
- python - 在 matplotlib 中绘制不同列大小数组的一维数组