谁能告诉我,如果使用另一种使用..其它..内部条件的反应对象如何,如果其他条件中反应物
const background = {
color: {
if (this.props.status == 'broker') {
background : '#FFFFFF'
} esle {
background : 'green'
}
}
}
谁能告诉我,如果使用另一种使用..其它..内部条件的反应对象如何,如果其他条件中反应物
const background = {
color: {
if (this.props.status == 'broker') {
background : '#FFFFFF'
} esle {
background : 'green'
}
}
}
您不需要全部内联评估它。嵌套的三元语句会变得非常难看。您可以创建一个函数并将结果传递给:
function bgColor(status) {
if (status === 'broker') {
return { background: '#fff' }
}
if (status === 'whatever' {
// return ...
}
// whatever else
}
const background = {
color: bgColor(this.props.status)
}
你可以使用一个三元:
const background = {
color: this.props.status === 'broker' ? '#ffffff' : 'green'
}
如果您需要更多条件,这可能会受到限制和/或难以阅读,因此您也可以使其发挥作用,但是您只需要小心如何/在哪里调用它:
const background = {
color: function() {
let color
if(this.props.status === 'broker') {
color = '#ffffff'
} else {
color = 'green'
}
return color
}
}
如果我想使用两个以上的条件呢? –
@radjeep嵌套ternarys? –
@RajdeepRatan我编辑了我的答案。你可以使用Jonas w建议的嵌套三元组,但如果这些三元组难以阅读,可以使你的属性成为一个函数。 –
您也可以使用二元运算符:'-5 || ''// -5' – fungusanthrax