【问题标题】:Does Lightning Web Component IF Directive support multiple conditions?Lightning Web 组件 IF 指令是否支持多种条件?
【发布时间】:2020-08-27 09:35:55
【问题描述】:

您好,我是 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>

当前使用上述语句

【问题讨论】:

    标签: javascript salesforce salesforce-lightning lwc


    【解决方案1】:

    选项 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

    这个答案也很酷:https://salesforce.stackexchange.com/q/249293/799

    【讨论】:

    • 选项 3 工作。感谢您阐明选项 2 的方法。
    猜你喜欢
    • 1970-01-01
    • 2016-09-11
    • 2014-07-13
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多