vue.js - 如何遍历数组数据并为 vue 中的每个新数据创建新行?
问题描述
我有一个我调用的流数据,axios.get
它resposne.data
是一个带有一个传感器数据的json,就像,{"Temperature":"56"},
我用来在vue模板中创建一行一样。resposne.data
分配给数组但它没有附加行,表中的数据正在改变。
这是模板部分
<template>
<div class="container">
<h1>Tabular Data</h1>
<table class="table table-striped table-bordered" >
<thead>
<tr>
<th class="text-center">Time</th>
<th class="center">Device Parameter</th>
<th class="center">Magnitude</th>
<th class="right">Unit</th>
</tr>
</thead>
<tbody>
<tr v-for="data in Tabular" :key="data">
<td> {{new Date().toString()}} </td>
<td class="test-left">Temperature</td>
<td class="text-center"> {{data.toString()}} </td>
<td class="test-left">C</td>
</tr>
<!-- <td>{{data_alias.Humidity}}</td>
<td>{{data_alias.Pressure}}</td> -->
</tbody>
</table>
</div>
</template>
<script>
/* eslint-disable */
import axios from 'axios';
export default {
name: 'tabular',
data() {
return {
Tabular:[ ],
}
},
mounted() {
setInterval(()=>{
axios.get('http://localhost:3000/data')
.then((response)=>{
console.log(response.data);
this.Tabular=response.data;
})
.catch((error)=>{
console.log(error);
});
},2000);//I used set interval to get data from stream constantly
},
}
</script>
解决方案
你确定要Tabular
填一个吗?我认为没有,因为this.Tabular=response.data;
;
this.Tabular 是undefined
;
尝试这个
var that = this;
axios.get('http://localhost:3000/data')
.then((response)=>{
console.log(response.data);
that.Tabular=response.data;
})
.catch((error)=>{
console.log(error);
});
推荐阅读
- flutter - Flutter Swiper:如何在左对齐时在一页上显示多个小部件?
- javascript - 如何检查字符串是否有元音?
- javascript - 如何在 Chrome JS 控制台中下载文件 url 列表
- javascript - 为什么数组中的某些声音没有暂停?
- python - PyDub:缺少 ffmpeg 标头
- html - 将表格列数据作为电子邮件地址
- json - 根据同一 JSON 文件中的另一个数组更新一个数组中的值
- r - R:存储要由函数返回的变量内容,如果重新分配对象,则该函数不会更改
- selenium - selenium.common.exceptions.NoSuchElementException:消息:无法找到元素 [id=""]
- conda - The Littlest JupyterHub:使用多通道在 Hyper-V 上安装错误