switch 和 if的区别
let applytypeText = "";
switch (detailObj.applytype) {
case 10:
applytypeText = "新增";
break;
case 20:
applytypeText = "更改";
break;
case 30:
applytypeText = "借用";
break;
default:
applytypeText = "";
}
<FormItem
label="申领方式:"
{...layoutOpt}
required={false}
errorMsg={getFieldError("AppMethod")}
>
<FormControl
{...getFieldProps("applytype", {
validateTrigger: "onBlur",
initialValue: applytypeText,
})}
/>
</FormItem>
applytypeText 值, 不渲染到页面上
let newApplytype = '';
if (detailObj.applytype == 30) {
newApplytype = '借用';
} else if (detailObj.applytype == 20) {
newApplytype = '更换'
} else if (detailObj.applytype == 10) {
newApplytype = '新增'
}
<FormItem
label="申领方式:"
{...layoutOpt}
required={false}
errorMsg={getFieldError("AppMethod")}
>
<FormControl
{...getFieldProps("applytype", {
validateTrigger: "onBlur",
initialValue: newApplytype,
})}
/>
</FormItem>
newApplytype值, 不渲染到页面上
有什么区别?
switch属于对字符串和数字 在严格模式下进行 判断, 得到的结果是 applytypeText = '' // 空字符串
而if判断 可以对 类型进行判断,则用 === 全等符号(及严格模式) 该if判断为 不对类型进行校验, == 不全等符号(非严格模式)
得到的结果 分为 三种 30/借用 20/更改 10/新增