Detecting Component Mappings

You can detect whether a component is part of a template using the isTemplateMapped method.

This is useful if you want to execute custom logic depending on whether the component was mapped in a template (injected via the @Id or annotation) or created directly using one of its constructors, for example.

Example: Calling the isTemplateMapped method in MyComponent.

Java

  1. @Tag("div")
  2. public class MyComponent extends Div {
  3. public MyComponent() {
  4. if (!isTemplateMapped()) {
  5. getElement().getStyle().set("color", "red");
  6. }
  7. }
  8. }
  • By default, the component only sets a style when the component is not part of a template.

See Binding Template Components for more about To @Id mappings.