我正在尝试获取总结果长度,但在我的模板中没有得到任何内容。这是我的脚本:
data() {
return {
searchResults: [],
totalResults: [],
}}
const response = await axios.post(
"http://localhost:5000/api/search",
searchData
);
this.searchResults = response.data.Response.Results; // Set the search results in the component's data // Retrieve the traceId from the response
const nestedResults = response.data.Response.Results;
const totalResults = nestedResults[0].length;
console.log("Total Results:", totalResults);
这是我的控制台,我得到了totalResults。
Total Results: 12
这是我的模板。
<p>Total Results: {{ totalResults }}</p>
模板返回了这个。
Total Results: []
我在我的模板中什么都得不到,请问我该怎么办?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
首先,你正在用一个空数组来初始化一个应该是数字的变量。你应该这样写:
data() { return { searchResults: [], totalResults: 0, }}其次,你没有将值赋给正确的totalResults变量,你只是声明了一个新变量。要将值赋给totalResults,你应该使用this.totalResults。因此,正确的写法是: