<p><span style="color:#383D41;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";font-size:16px;white-space:normal;background-color:#FFFFFF;">我正在尝试获取总结果长度,但在我的模板中没有得到任何内容。这是我的脚本:</span></p>
<pre class="brush:php;toolbar:false;">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);</pre>
<p>这是我的控制台,我得到了totalResults。</p>
<pre class="brush:php;toolbar:false;">Total Results: 12</pre>
<p>这是我的模板。</p>
<pre class="brush:php;toolbar:false;"><p>Total Results: {{ totalResults }}</p></pre>
<p>模板返回了这个。</p>
<pre class="brush:php;toolbar:false;">Total Results: []</pre>
<p>我在我的模板中什么都得不到,请问我该怎么办?</p>            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
         
        
        
首先,你正在用一个空数组来初始化一个应该是数字的变量。你应该这样写:
data() { return { searchResults: [], totalResults: 0, }}其次,你没有将值赋给正确的totalResults变量,你只是声明了一个新变量。要将值赋给totalResults,你应该使用this.totalResults。因此,正确的写法是: