javascript - Vue模板中的数组未更新
问题描述
我正在我的 vue 中创建一个动态数组。此数组正在从 JSON 数组的脚本中填充,但未在模板中更新。在模板中它返回空白数组。这是我的代码:
<template>
<div>
{{users}} <!-- this is returning blank -->
</div>
</template>
<script>
export default {
data(){
return {
users: []
}
},
created(){
this.fetchUsers();
},
methods:{
fetchUsers(){
fetch('api/single')
.then(res => res.json())
.then(res => {
this.users= res.data;
console.log(this.users); //this is showing the data
})
.catch(err => console.log(err));
}
}
};
</script>
解决方案
Fetch API 发送异步请求,因此在从 API 加载数据之前显示用户。调用绑定到用户的计算方法。
推荐阅读
- laravel - 在 LARAVEL 中保存并显示来自外部网站的 POST 请求
- excel - 复制/粘贴到 Word 文档中的 Excel VBA 代码导致我的代码失败
- python - 查找非周末假期
- rest - How to create a Junit test for the following controller?
- pthreads - openmp pthread support for avr-gcc
- python - Combine output from 2 pandas groupbys? Is it possible to combine the two lines of code below so that I have the std dev and skew in the same table?
- php - Cannot add NOT NULL column with default value NULL
- webpack - Convert inline plugin webpack 4 to webpack 5
- python - 或指定多个并行工作人员超过 1 的工具返回错误
- typescript - 推断多个属性的类型相同