Component decorators

Aurelia provides a comprehensive set of decorators that allow you to customize the behavior of custom elements and components. These decorators give you fine-grained control over how components are created, rendered, and interact with their environment.

Core Component Decorators

@customElement

The @customElement decorator allows you to explicitly define a custom element and override naming conventions.

import { customElement } from 'aurelia-framework';

@customElement('my-widget')
export class SpecialWidget {
  // Component logic
}

Usage:

<my-widget></my-widget>

Benefits:

  • Override default naming conventions

  • Create more semantic element names

  • Avoid naming conflicts

@bindable

The @bindable decorator makes properties available for data binding from parent components.

Configuration Options:

  • defaultBindingMode: Controls data flow direction

  • primaryProperty: Makes this the default property for the element

  • changeHandler: Specifies a custom change callback method name

View and Template Decorators

@useView

Specifies an alternative view file for the component.

Use Cases:

  • A/B testing different layouts

  • Conditional template selection

  • Shared view-models with different presentations

@inlineView

Defines the component's template inline within the JavaScript/TypeScript file.

Benefits:

  • Single-file components

  • Better for simple components

  • Eliminates need for separate .html files

Advanced Usage with Dependencies:

@noView

Indicates that the component handles its own rendering and doesn't need a view file.

DOM and Rendering Decorators

@containerless

Removes the custom element wrapper from the rendered output.

Template:

Usage and Output:

Important Limitations:

  • Cannot use with @child or @children

  • Cannot use with @useShadowDOM

  • Cannot use with surrogate behaviors

  • Use sparingly as it can affect styling and event handling

@useShadowDOM

Renders the component using Shadow DOM for complete style encapsulation.

Template:

Advanced Shadow DOM with DOMBoundary:

Content and Children Decorators

@child

Creates a reference to a single child element matching the selector.

Template:

Limitations:

  • Cannot use with @containerless

  • Only selects immediate children

  • Returns the first matching element

@children

Creates an array of all child elements matching the selector.

Template:

Advanced Usage with Dynamic Children:

Content Processing Decorators

@processContent

Controls how the component's content is processed during compilation.

Advanced Content Processing Example:

Practical Examples

Media Player Component

Data Visualization Component

Usage:

Best Practices

1. Choose the Right Decorator Combination

2. Use @child and @children Appropriately

3. Manage Memory in Custom Decorators

4. Use @processContent Judiciously

Component decorators in Aurelia provide powerful ways to customize component behavior, but they should be used thoughtfully. Understanding when and how to use each decorator will help you create more maintainable and efficient components.

Last updated

Was this helpful?