javascript - 返回最后一个值而不是正确的值
问题描述
我想要的是每个名称都会获得它的关联值,但是下面的代码将打印每个循环上的所有值,然后它会在最后一个值处停止。
主页.html
<template>
<div repeat.for="name of names">
${getValue(name)}
<p>Hello ${value}</p>
</div>
</template>
主页.js
export class Home {
names = ["one", "two", "three"];
constructor() {
this.getValue();
this.value = "";
}
getData() {
return httpClient.fetch('example.com/api/v')
.then(response => response.json())
.then(data => {
return data;
});
}
getValue(name) {
this.getData()
.then(data => this.value = data.name);
}
}
实际结果:
Hello value3 //It prints value1 value2 then stops at value3
Hello value3 //It prints value1 value2 then stops at value3
Hello value3 //It prints value1 value2 then stops at value3
预期成绩:
Hello value1
Hello value2
Hello value3
解决方案
推荐阅读
- javascript - asyncData 中的 Vuex 和 Axios
- python - 随机森林回归 MAE
- angular - 我有角度的类模型有变量和函数,所以当我调用函数时它给了我错误
- ruby-on-rails - Heroku 服务器上的数据库中似乎不存在类别和子类别
- python - 有没有办法从 PyPI 访问搜索结果而无需抓取?
- android - 从 webview 加载的在线页面超链接到应用程序的本地 html 页面
- python - 如何使用pyglet创建免费游戏碰撞?
- html - div 在所有桌面上的位置不一致
- android - 如何使用 Coroutines 在 Kotlin 中正确初始化 Recyclerview?
- django - Django Rest Framework - 删除问题 - 找不到 CSRF