如何在Vue.js中取得數組的總長度
P粉908138620
P粉908138620 2023-07-27 21:43:09
0
1
588
<p><span style="color:#383D41;font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-ser Colorif, "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>
P粉908138620
P粉908138620

全部回覆(1)
P粉668146636

首先,你正在用空數組來初始化一個應該是數字的變數。你應該這樣寫:

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

其次,你沒有將值賦給正確的totalResults變量,你只是聲明了一個新變數。要將數值賦給totalResults,你應該使用this.totalResults。因此,正確的寫法是:

this.totalResults = nestedResults[0].length;
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!