![](/img/trans.png)
[英]How to properly use a ternary expression with @click event in Vue.js
[英]Vue JS ternary expression
我正在使用 Vue JS 並嘗試使用三元表達式來有條件地更改某些東西的值,我正在努力將以下內容轉換為三元表達式,這是我的方法,默認情況下: isLoading
為 true
fetchData(showLoading) {
if (showLoading) {
this.isLoading = true
} else {
this.isLoading = false
}
}
不要在這里使用條件運算符,只需將showLoading
分配給isLoading
,假設您正在傳遞一個布爾值:
this.isLoading = showLoading;
如果您不一定要傳遞布爾值,則首先轉換為布爾值(如果需要):
this.isLoading = Boolean(showLoading);
如果您必須使用條件運算符,它將是:
this.isLoading = showLoading ? true : false;
fetchData(showLoading) {
showLoading ? (this.isLoading = true) : (this.isLoading = false)
}
你的“showLoading”數據我猜是布爾值,否則你不需要。
this.isLoading = showLoading
下面是錯誤的代碼會出錯
this.isLoading = showLoading ? true : false;
看起來你想強制 showLoading 是一個布爾值所以試試這個
this.isLoading = !!showLoading;
this.showLoading = isLoading ? true : false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.