javascript - 在mounted()中运行循环时浏览器卡住
问题描述
我正在尝试打开一个端点mounted()
,我想增加 id 直到它为假,这样我就可以在页面上显示所有结果
data () {
return {
id : 0,
found : true,
}
},
mounted(){
while(this.found){
this.$http.get(`https://jsonplaceholder.typicode.com/todos/${this.id}`)
.then((data)=>{
this.characters = data.body
this.id++;
})
console.log(this.id);
}
为什么浏览器会卡住?我的循环中有什么东西吗?
解决方案
我的循环中有什么东西吗?
是的:你永远不会this.id
在循环中改变。该循环只是在 HTTP 请求之后的 HTTP 请求之后创建 HTTP 请求,因为您的增量this.id
在您的回调中then
,这是异步的。
将增量移到then
回调之外,以便循环真正完成。
将您的编辑替换while (this.id <= 10)
为while (this.found)
,这是同样的问题:没有任何设置this.found
。
推荐阅读
- html - 如何创建命令以在 Windows Batch 中运行其他应用程序?
- javascript - 如何在javascript循环中循环django模型?
- haskell - Haskell - 拉链替代品
- mysql - MySQL:在 GROUP BY 中求和和减法
- reactjs - React/Redux - 查看将存储中的数组视为对象
- c# - “虽然!= -1”-1 错误地出现在我的计算中(C#)
- r - 如何根据 x 轴使用 r 代码在图形上绘制不同的颜色
- java - Java Collection 实现有这些特点吗?
- c# - asp.net GetStringAsync 有时返回 null
- javascript - 有没有办法以 JSON 格式获取谷歌搜索结果?