【发布时间】:2019-10-10 16:19:01
【问题描述】:
我正在尝试访问 vue 组件计算属性中的 Object 属性,但我收到一条错误消息,指出该属性未定义。我已经设置了原型here。当我尝试使用时
playerEntry.stats.RecYards["#text"] > "0"
Vue 抱怨
[Vue 警告]:渲染错误:“TypeError: playerentry.stats.RecYards is undefined”
在 Computed 下查看 vue.js devtools 我看到 playerReceivingStats:"(error during evaluation)" 这是相关的代码:
boxscore.js
const boxScoresStats = {
stats: Vue.component("box-scores", {
props: ["props_box_score", "props_gameID"],
data: function() {
return {
playerStats: this.props_box_score.data.gameboxscore.awayTeam.awayPlayers
.playerEntry
};
},
computed: {
playerPassingStats: function() {
return this.playerStats.filter(playerEntry => {
return playerEntry.player.Position === "QB";
});
},
playerReceivingStats: function() {
return this.playerStats.filter(playerEntry => {
console.log(playerEntry.stats.RecYards["#text"]);
return playerEntry.stats.RecYards["#text"] > "0";
});
}
},
模板
<div v-for="playerStats in playerReceivingStats">
<tr class="d-flex">
<td class="col-3 justify-content-center" scope="row">
{{playerStats.player.FirstName}} {{playerStats.player.LastName}} ({{playerStats.player.Position}})
</td>
<td class="col-2 justify-content-center" justify-content="center">
{{ playerStats.stats.Receptions['#text'] }} </td>
<td class="col-3 justify-content-center">{{playerStats.stats.RecYards['#text']}}</td>
<td class="col-2 justify-content-center">{{playerStats.stats.RecTD['#text']}}</td>
<td class="col-2 justify-content-center">{{playerStats.stats.Targets['#text']}}</td>
</tr>
</div>
我尝试了括号和点符号,但仍然没有好。我看到 console.log 中的值打印正常。但是当我可以在 devtools 中看到对象属性时,我不知道为什么它说未定义。另请注意,如果我将计算中的 eval 缩短为 playerEntry.stats.RecYards 我没有收到错误?但是我得到了不正确的结果。非常感谢任何帮助。
更新:
这是有效的计算属性:
playerReceivingStats: function() {
return this.offensivePlayers.filter(playerEntry => {
if (typeof playerEntry.stats.RecYards != "undefined") {
return playerEntry.stats.RecYards["#text"] > "0";
}
});
}
}
【问题讨论】:
-
该播放器缺少
RecYards属性。当我尝试它时,它似乎是Logan Cooke,id14702,this.playerStats[11]。您看到的日志记录是针对前几个玩家的,它们运行良好,但是当它到达该玩家时它就会崩溃。 -
尝试将
playerStats设置为计算属性而不是数据属性。 -
关于更新:缺少相关字段的是
playerStats[11]。图为playerStats[0]。
标签: javascript vue.js object-notation