如何在Vue.js中获取数组的总长度
P粉908138620
P粉908138620 2023-07-27 21:43:09
[Vue.js讨论组]

我正在尝试获取总结果长度,但在我的模板中没有得到任何内容。这是我的脚本:

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: []

我在我的模板中什么都得不到,请问我该怎么办?

P粉908138620
P粉908138620

全部回复(1)
P粉668146636

首先,你正在用一个空数组来初始化一个应该是数字的变量。你应该这样写:

data() {
    return {
      searchResults: [],
      totalResults: 0,
}}

其次,你没有将值赋给正确的totalResults变量,你只是声明了一个新变量。要将值赋给totalResults,你应该使用this.totalResults。因此,正确的写法是:

this.totalResults = nestedResults[0].length;
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号