简体   繁体   English

获取的数据未填充在表 [vue] 中

[英]Fetched data is not populated in table [vue]

I am new to js/vue and I am trying to fetch data from an API. I have a field which value will be used to fetch data from the API for that keyword.我是 js/vue 的新手,我正在尝试从 API 中获取数据。我有一个字段,该字段的值将用于从该关键字的 API 中获取数据。 I can see in the console log that I do get the data as an array.我可以在控制台日志中看到我确实将数据作为数组获取。 However, that data does not get populated in the table.但是,该数据不会填充到表中。

The odd thing that is worth mentioning is that if I make a slight change in the code, such as delete extra space and save...while I still have the browser open with the data fetched, then the table will get populated.值得一提的奇怪之处在于,如果我对代码稍作更改,例如删除额外空间并保存...而我仍然打开浏览器并获取数据,那么表格将被填充。

In the script I have:在脚本中我有:

let data;

const fetchData = async (inputString: string) => {
 data = await getData(inputString);
 console.log('Data', data);
 return data;
}

And the input field + button: <input v-model='inputString' placeholder='Write keyword here' /> <button action @click='fetchData(inputString)"> Fetch data </button>以及输入框+按钮: <input v-model='inputString' placeholder='Write keyword here' /> <button action @click='fetchData(inputString)"> Fetch data </button>

Your data variable needs to be reactive for the template to know about changes.您的数据变量需要对模板具有反应性才能了解更改。

import { ref } from 'vue';  
const data = ref<null|string>(null);

const fetchData = async (inputString: string) => {
 data.value = await getData(inputString);
 console.log('Data', data);
 return data.value;
}

In your template you can then simply use data like this:在您的模板中,您可以简单地使用这样的数据:

<div>{{data}}</div>

See more about reactivity here: https://vuejs.org/guide/essentials/reactivity-fundamentals.html#reactive-variables-with-ref在此处查看有关反应性的更多信息: https://vuejs.org/guide/essentials/reactivity-fundamentals.html#reactive-variables-with-ref

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM