Component lifecycles
Last updated
Was this helpful?
Last updated
Was this helpful?
Aurelia components offer a rich lifecycle that lets you hook into specific moments of a component's existence—from construction, through activation, to eventual disposal. Understanding the order and intent of each hook will help you write components that are predictable, testable, and memory-leak-free.
Construction
constructor
once
–
–
define
once
top ➞ down
no
hydrating
once
top ➞ down
no
hydrated
once
top ➞ down
no
created
once
bottom ➞ up
no
Activation
binding
every activation
top ➞ down
yes (blocks children)
bound
every activation
bottom ➞ up
yes (awaits)
attaching
every activation
top ➞ down
yes (awaits before attached
)
attached
every activation
bottom ➞ up
yes (awaits)
Deactivation
detaching
every deactivation
bottom ➞ up
yes (awaits before DOM removal)
unbinding
every deactivation
bottom ➞ up
yes (awaits)
Cleanup
dispose
when permanently discarded
–
–
Legend
top ➞ down – parent executes before its children
bottom ➞ up – children execute before their parent
Executed when the instance is created. Inject services here and perform work that does not depend on bindable values.
Opportunity to modify the component definition before hydration begins.
Can return a partial definition to override aspects of the component's behavior.
Runs synchronously, parent before children.
Opportunity to register dependencies in controller.container
that are needed while compiling the view template.
Runs synchronously, parent before children.
View template has been compiled, child components are not yet created.
Last chance to influence how the soon-to-be-created child components resolve their dependencies.
All child components are now constructed and hydrated.
Executes once per instance, children before parent.
Great for logic that must run after the whole subtree is constructed but before binding.
Bindable properties have been set but bindings in the view are not yet connected.
Runs parent ➞ child.
Return a Promise
(or mark the method async
) to block binding/attaching of children until resolved.
View-to-view-model bindings are active; ref
, let
, and from-view
values are available.
Executes child ➞ parent.
The component's host element is now in the DOM but child components may still be attaching.
Queue animations or setup 3rd-party libraries here.
A returned Promise
is awaited before attached
is invoked on this component but does not block children.
The entire component subtree is mounted; safe to measure elements or call libraries that need actual layout information.
Executes child ➞ parent.
Note: Only receives the initiator
parameter, not the parent.
Called when the framework removes the component's element from the DOM.
Executes child ➞ parent. Any returned Promise
(e.g., an outgoing animation) is awaited in parallel with sibling promises.
Runs after detaching
finishes and bindings have been disconnected.
Executes child ➞ parent.
Invoked when the instance is permanently discarded—typically when removed from a repeater and the view cache is full, or when the application shuts down.
Use to tear down long-lived resources, subscriptions, or manual observers to prevent memory leaks.
@lifecycleHooks
)For cross-cutting concerns like logging, analytics, or debugging, implement lifecycle hooks in a separate class using the @lifecycleHooks
decorator. This keeps your component code focused while adding shared behavior.
Multiple lifecycle hook classes can be registered; the framework executes them in registration order alongside the component's own lifecycle methods.
<au-compose>
components additionally support activate
/ deactivate
hooks—see the dynamic composition guide.
Router hooks such as canLoad
, loading
, canUnload
, unloading
, etc., are documented in the routing lifecycle section and are available even if you do not use the router.
Prefer early exits—perform checks at the start of hooks and return
early to minimise nesting.
Clean up observers, timeouts, event listeners, or 3rd-party widgets in the opposite hook (unbinding
/detaching
or dispose
).
Avoid heavy work in the constructor. Move anything needing bindables or DOM to later hooks.
Mark hooks async
and await
your operations instead of manually creating Promises for clarity.
Keep hooks fast—expensive work can block the component hierarchy.