Skip to content
Advertisement

Conditional rendering in Polymer 3

I need to render different html based on a bool variables true or false value. For example in react I would do something like this in my return in the render function:

JavaScript

Where I get two different output depending on the value of booleanValue.

I tried in Polymer 3, and first declared my bool variable:

JavaScript

I then try to use it in my template/html as

JavaScript

However, the code does not recognize the variable “this.myBoolValue” in the html template. How come? Full code of my template:

JavaScript

Advertisement

Answer

If your default value of myBoolValue is false you can change your properties and template like this (If you want to use conditional templates you have to import @polymer/polymer/lib/elements/dom-if.js.)

JavaScript

If you can’t or don’t want to set a default value change your code like this and use a computed property:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement