简体   繁体   English

如何更改 Lit-Element 中的 Boolean 属性以隐藏组件上的内容?

[英]How can I change a Boolean property in Lit-Element to hide content on my component?

I have a custom component that should hide content when I set a boolean property to false.当我将 boolean 属性设置为 false 时,我有一个自定义组件应该隐藏内容。 Every other property gets reflected except that one.除那个属性外,所有其他属性都会得到反映。 I must be doing something wrong.我一定做错了什么。

static get properties(){
      title: {
        type: String,
        attribute: 'title',
      },

      titleEnable: {
        type: Boolean,
        attribute: 'title-enable',
      },
}

constructor() {
    super();
    this.titleEnable=true;
    this.title="default";
}

render(){

    return html`                
        ${this.titleEnable 
        ? html`
        <p class="title" ?hidden="${!this.titleEnable}">${this.title}</p>
        `
        : html ``} 
    ` 
}

If I use that component like <my-component></my-component> in an HTML file it shows: default as expected.如果我在 HTML 文件中使用像<my-component></my-component>这样的组件,它会显示: default as expected。

If I use it like this: <my-component title="New Title"></my-component> it displays: New Title as expected.如果我这样使用它: <my-component title="New Title"></my-component>它会显示: New Title as expected。

BUT if I try to hide it <my-component title-enable="false"></my-component> the boolean just doesn't change.但是如果我试图隐藏它<my-component title-enable="false"></my-component> boolean 就不会改变。 I've tried,title-enable, title-enable='false". .titleEnable=false and all the variants you can imagine. What pisses me the most is that whenever I set in the constructor 'this.titleEnable=false' and I happen to just declare the variable WITHOUT value on the tag and it takes it as TRUE an "default" appears. <my-component title-enable></my-component> I am completely lost.我已经尝试过,title-enable,title-enable='false'..titleEnable=false 和你能想象到的所有变体。最让我生气的是,每当我在构造函数中设置'this.titleEnable=false'和我碰巧只是在标签上声明了变量 WITHOUT value,它认为它是 TRUE,出现“默认”。 <my-component title-enable></my-component>我完全迷路了。

Ok, this is a tricky one.好吧,这是一个棘手的问题。 You need to handle it differently by passing some object as below:您需要通过传递一些对象来以不同的方式处理它,如下所示:

  static get properties() {
    return {
      titleConfig: {
        type: Object,
        attribute: 'title-config'
      }
    }
  }

  render() {
    return html`                
      ${this.titleConfig.titleEnable
        ? html`
      <p class="title" ?hidden="${!this.titleConfig.titleEnable}">${this.titleConfig.title}</p>
      `
        : html``} 
  `
  }

In HTML :HTML 中

<my-component title-config='{"title":"shan", "titleEnable": false}'></my-component>

Now, the question is why it is true every time?现在,问题是为什么每次都是true

Answer: For a Boolean property to be configurable from markup, it must default to false.回答:对于可从标记配置的布尔属性,它必须默认为 false。 If it defaults to true, you cannot set it to false from markup, since the presence of the attribute, with or without a value, equates to true.如果它默认为 true,则不能通过标记将其设置为 false,因为属性的存在,无论是否有值,都等于 true。 This is the standard behavior for attributes in the web platform.这是 Web 平台中属性的标准行为。

It is taken from polymer doc .它取自聚合物文档

So, by creating an attribute title-enable , the HTML considers this attribute as true因此,通过创建属性title-enableHTML认为该属性为true

It's really a bummer for someone who starts working on Polymer or LitElement .对于开始研究 Polymer 或 LitElement 的人来说,这真的是一个无赖。

According to the documentation :根据文档

To bind to a boolean attribute you need to use the "?", notation:要绑定到布尔属性,您需要使用“?”,表示法:

Bind prop3 to a boolean attribute:将 prop3 绑定到一个布尔属性:

html`<input type="text" ?disabled="${this.prop3}">`

Boolean attributes are added if the expression evaluates to a truthy value, and removed if it evaluates to a falsy value.如果表达式计算为真值,则添加布尔属性,如果计算为假值,则删除布尔属性。

You can also use reflected attributes instead:您也可以改用反射属性

static get properties(){
      title: {
        type: String,
        attribute: 'title',
      },

      titleEnable: {
        reflect: true,
      },
}

constructor() {
    super();
    this.titleEnable='true';
    this.title="default";
}

handleHidden() {
  return this.titleEnable === 'true'
}

render(){

    return html`                
        ${this.titleEnable 
        ? html`
        <p class="title" ?hidden="${!this.handleHidden()}">${this.title}</p>
        `
        : html ``} 
    ` 
}

This issue is because of data type.这个问题是因为数据类型。 In lit-element 'false' |在 lit-element 'false' | 'true' the type is returned as 'string' data type. 'true' 类型作为 'string' 数据类型返回。

Solution:解决方案:

Before that check your attribute data type is string or boolean

     ex: typeof attr

If it return string means, you have two solution:
    1. Change the attributes date type as boolean ex: Boolean(attr).
    2. Consider the value as string and check your condition.

From your example 
    if(this.titleConfig.titleEnable == 'true'){
        //true statement
    }else{
        // false statement
    }

my suggestion is this.titleConfig.titleEnable == 'true'我的建议是 this.titleConfig.titleEnable == 'true'

Instead of passing your value as an attribute you also could pass it as a property ( https://lit.dev/docs/templates/expressions/#property-expressions ):除了将您的值作为属性传递外,您还可以将其作为属性传递 ( https://lit.dev/docs/templates/expressions/#property-expressions ):

<my-component .title-enable=${false}></my-component>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM