Javascript 如何在java脚本中的工具栏中使用if条件
我是react native和java脚本的新手。我想检查一下,当我的工具栏中的项目等于“A”(当某个实体选择A)时,可触摸属性变为false。我是这样写的:Javascript 如何在java脚本中的工具栏中使用if条件,javascript,react-native,Javascript,React Native,我是react native和java脚本的新手。我想检查一下,当我的工具栏中的项目等于“A”(当某个实体选择A)时,可触摸属性变为false。我是这样写的: init: function( pThis ) { toolbarActiveItem: 'test', this.state = { toolbar: [ { id: 5, name: 'test
init: function( pThis ) {
toolbarActiveItem: 'test',
this.state = {
toolbar: [
{
id: 5,
name: 'test',
description: I18n.t('test'),
if(this.item=='A'){
isTouchable: false,
}
else {
isTouchable: true,
}
onOpen: (item) => { this.onToolbarOpen(pThis,item) },
},
],
};
this.onToolbarOpen(pThis);
},
然而,我得到了语法错误。我应该提到的是,代码在没有if条件的情况下工作。你能帮我解决这个问题吗?< /p> 你不能在对象文字的中间放一个<代码>如果语句。< /P> 您可以分两个阶段构建它:
var obj = {};
if (foo) {
obj.bar = 1;
} else {
obj.bar = 2;
}
// then pass `obj` somewhere
可以使用三元运算符:
isTouchable: ( this.item=='A' ? true : false ),
…但由于等式比较会给您一个布尔值,因此最简单的方法是分配:
isTouchable: ( this.item == 'A' )
在对象文字的中间,不能将<代码>放在语句中。
您可以分两个阶段构建它:var obj = {};
if (foo) {
obj.bar = 1;
} else {
obj.bar = 2;
}
// then pass `obj` somewhere
可以使用三元运算符:
isTouchable: ( this.item=='A' ? true : false ),
…但由于等式比较会给您一个布尔值,因此最简单的方法是分配:
isTouchable: ( this.item == 'A' )
工具栏
元素是一个对象,其中不能使用if语句。您必须使用前面声明的变量,以便以后填写:
init: function( pThis ) {
var touchable;
if(this.item=='A'){
touchable = false,
}
else {
touchable = true,
}
toolbarActiveItem: 'test',
this.state = {
toolbar: [
{
id: 5,
name: 'test',
description: I18n.t('test'),
isTouchable: touchable,
onOpen: (item) => { this.onToolbarOpen(pThis,item) },
},
],
};
this.onToolbarOpen(pThis);
},
工具栏
元素是一个对象,其中不能使用if语句。您必须使用前面声明的变量,以便以后填写:
init: function( pThis ) {
var touchable;
if(this.item=='A'){
touchable = false,
}
else {
touchable = true,
}
toolbarActiveItem: 'test',
this.state = {
toolbar: [
{
id: 5,
name: 'test',
description: I18n.t('test'),
isTouchable: touchable,
onOpen: (item) => { this.onToolbarOpen(pThis,item) },
},
],
};
this.onToolbarOpen(pThis);
},
回答得很好!非常感谢你。还有一个问题,如果我有两个条件呢。例如,如果我需要同时检查A和B项,该怎么办?回答得很好!非常感谢你。还有一个问题,如果我有两个条件呢。例如,如果我需要同时检查A和B的项目,该怎么办?