问题描述
嗨,我是LWC世界及其语法的新手。 我想问一下,LWC是否在单个语句中支持多个条件?
例如:
<template if:true={condition1 == 'value1' && condition2 == 'value2'}></template>
在单行语句上使用多个条件时出错。
或 应该采用以下方法
html - <template if:true={validateCondition}></template>
class js file - get validateCondition() { return (condition1 == 'value1' && condition2 == 'value2');}
或者必须使用嵌套条件
<template if:true={condition1 == 'value1'}>
<template if:true={condition2 == 'value2'}>
</template>
</template>
当前使用上述语句
解决方法
选项2。保持html干净。显示/隐藏页面部分的决定是业务逻辑的一部分,应与其他计算一起保存在JS中。这也使其可测试。我认为您甚至无法执行选项3,我希望它在编译时会失败?
您不能在这些或任何计算出的内容中使用公式,只能使用直接属性和吸气剂。 https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_directives
迁移指南也可能会有所帮助:https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.migrate_expressions