【发布时间】:2020-11-18 10:56:53
【问题描述】:
我觉得这是一个计时的事情,但不一定是异步的事情。我正在循环一个对象并显示一个项目列表。对于其中一个值,我需要用一种方法来计算。
直接在项目对象上显示的值很好,但计算出来的值永远不会显示,即使我可以 console.log 它并且它就在那里。
我正在尝试更改键顶部以重新呈现列表,但没有运气。我尝试将其设为计算属性,但遇到了“不是函数”的问题。
<ul>
<li
v-for="(item, index) in list"
:key="index"
class="list-wrap"
>
<span>
{{ item.name }} <---- this value shows every time.
</span>
<span class="location">
{{ getLocation(item.Location[0]) }} <---- this "calculated" value returns only sometimes.
</span>
</li>
</ul>
getLocation 方法:
methods: {
getLocation(loc) { // id coming from item in loop
this.locations.forEach((location) => { // loop through locations obj, match id, return location name.
let match;
if (location.id === loc) {
match = location.name;
console.log(match); <---- present / correct on every refresh
return match; <--- not rendering
}
});
},
},
// 列表是在异步 api 调用中创建的
async getCurUserTransfers() {
await airtableQuery
.getTableAsync("Transfers", 100, "Grid view")
.then((data) => {
this.list = data.filter( // list is a filtered table.
(transfer) =>
transfer.fields.User === this.curUserNicename ||
transfer.fields.User === this.curUserDisplayName
);
});
},
【问题讨论】:
标签: javascript vue.js vuejs2 vue-component v-for