javascript - VueJS 在页面加载时处理多个承诺
问题描述
我已经搜索了很多问题,但仍然没有找到解决方法,所以你是我最后的机会......
我有一个 Vue 组件,他需要在创建时从 API 加载数据,然后在组件中显示这些数据。我之前已经这样做了,没有任何问题,但是对于我正在制作的这个,它不起作用......
<template>
<thead v-if="loaded">
<tr>
<th v-for="label in labels">
{{label}}
</th>
</tr>
</thead>
</template>
<script>
import Schema from '../../api/schema'
export default {
data() {
return {
labels: [],
loaded: false
}
},
props: {
fields: Array
},
methods: {
getFieldsLabels: function () {
return Promise.all(
this.fields
.map((field) => Schema().getField(field)
.then(response => response.getLabel())
)
)
}
},
mounted() {
this.getFieldsLabels().then(
response => {
this.loaded = true;
this.labels = response
}
)
}
}
</script>
我有多个要传递的承诺,Promise.all
但是当我在浏览器上按 F5 时,页面仍然是空白的。但是当它用 HMR 重新加载时,它可以工作......
有任何想法吗 ?
解决方案
我想是因为你试图使用then
另一个人的回应then
试试看
getFieldsLabels: function () {
return Promise.all(
this.fields
.map((field) => Schema().getField(field))
)
}
您需要添加getLabel
到mounted
功能
this.getFieldsLabels().then(
response => {
this.loaded = true;
this.labels = response.getLabel()
}
)
推荐阅读
- google-ads-api - Google ads api 列出所有帐户及其广告系列
- c# - MySql.Data.MySqlClient.MySqlException 版本字符串格式错误,同时连接到 -log 启用实例
- php - 如何在没有依赖注入的情况下使用自定义 FormRequest 验证请求
- gradle - Gradle 7.2 版本目录指定库构建类型
- iis - IIS 一个请求在两个站点上
- r - 最大限度地减少重新分配个人的成本
- javascript - 我怎样才能等待乘以可观察对象以调用顺序完成?
- javascript - JavaScript 如何将此 innerText 转换为有效的日期格式?
- arrow-functions - 为什么箭头函数(第 35 行)返回未定义?
- python - 我以前的 MATLAB,现在是 Python 代码中出现这么多错误的原因是什么?