Vue JS三元表达式

Rya*_*ton 0 javascript vue.js vuejs2

我正在使用Vue JS,并尝试使用三元表达式有条件地更改某些值,我正在努力将以下内容转换为三元表达式,这是我的默认方法:isLoading是true

fetchData(showLoading) {
  if (showLoading) {
    this.isLoading = true
  } else {
    this.isLoading = false
  }
}

Run Code Online (Sandbox Code Playgroud)

Cer*_*nce 5

假设您要传递布尔值,请不要在此处使用条件运算符,只需分配给showLoading即可isLoading

this.isLoading = showLoading;
Run Code Online (Sandbox Code Playgroud)

如果您不一定要传递布尔值,请先将其强制转换为布尔值(如果需要):

this.isLoading = Boolean(showLoading);
Run Code Online (Sandbox Code Playgroud)

如果必须使用条件运算符,它将是:

this.isLoading = showLoading ? true : false;
Run Code Online (Sandbox Code Playgroud)