我目前正在嘗試在車把中為我的社交媒體應用程式的訊息傳遞部分撰寫一個 if else 塊。我正在傳遞來自控制器的訊息,對于我正在使用的示例,有一個空陣列,但由于某種原因它一直轉到 else 陳述句。我已經嘗試了許多不同的組合來實作這一點,但它一直在 else 陳述句中。我究竟做錯了什么?
{{#if messages.length}}
<p>if statement</p>
<div class="message-container card" id="{{this._id}}">
<p> Start a new conversation </p>
</div>
{{else}}
<p>else statement</p>
{{#each messages}}
<div class="message-container card" id="{{this._id}}">
<div class="profile-picture">
<img src={{user.profilePicture}} width="100"
height="100"
class="rounded-circle"/>
</div>
<div class="message-header">
<span class="name">{{this.sender.firstName}} {{this.sender.lastName}}</span>
<div class="date-sent">{{this.createdAt}}</div>
</div>
<div class="post-body">
<p class="message">
{{this.message}}
</p>
</div>
</div>
{{/each}}
{{/if}}
uj5u.com熱心網友回復:
是的。這是正確的。如果有一個空陣列,它應該轉到else陳述句,因為一個空陣列的長度0是falsy。
在您的代碼中,您有
{{#if messages.length}}
print this if array is not empty because
message.length is not zero
{{#else}}
print this if array is empty
because message.length is zero
{{/if}}
所以它的行為是正確的。
如果您打算做相反的事情,請交換ifandelse塊內的代碼或替換#if為#unless.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/459249.html
標籤:javascript 表示 猫鼬 车把.js
上一篇:貓鼬模式查找和更新陣列子檔案
