猿问

vue的jsx如何模仿v-if进行条件渲染。

例如在下面代码中的el-button按钮要根据某条件进行渲染时,vue的jsx目前我只晓得可以这么写,但当条件跟按钮一多时,这样写就会显得十分不堪,想请教下大神,jsx中如何写判断语句来决定某标签是否渲染呢?

let a = 1;

if (a === 1) {

    return (

        <div>

            <p>测试</p>

            <el-button>测试</el-button>

        </div>);

} else {

    return (

         <div>

            <p>测试</p>

        </div>);

}


HUX布斯
浏览 386回答 1
1回答

慕虎7371278

这样写就行了:let a = 1;&nbsp; return (&nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; <p>测试</p>&nbsp; &nbsp; &nbsp; &nbsp; {a === 1 ? <el-button>测试</el-button> : ''}&nbsp; &nbsp; </div>);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答