Learn how to use Aurelia's enhance feature to add interactivity to existing HTML, integrate with other frameworks, hydrate server-rendered content, and create multiple Aurelia instances in your applic
What is Enhancement?
Enhancement allows you to bring Aurelia's data binding, templating, and component features to existing DOM content without replacing it entirely. Instead of starting with an empty element and rendering into it, enhancement takes existing HTML and makes it "Aurelia-aware".
This is perfect for:
Progressive enhancement of server-rendered pages
Integration with existing applications or other frameworks
Widget development where you control specific sections of a page
Content Management Systems where you want to add interactivity to generated content
The startup sections showed how to start Aurelia for empty elements. Enhancement lets you work with existing DOM trees instead.
Before you start: Review App configuration and startup to understand the standard bootstrap flow; enhancement builds on those concepts.
Understanding What Gets Enhanced
When you enhance an element, Aurelia treats that element as if it were the template of a custom element. The existing HTML becomes the "template" and your component object becomes the "view model" that provides data and behavior.
Basic Enhancement Syntax
// Using the convenience method (recommended)constenhanceRoot=awaitAurelia.enhance({host:document.querySelector('#my-content'),component:{message:'Hello World'}});// Using instance method constau=newAurelia();constenhanceRoot=awaitau.enhance({host:document.querySelector('#my-content'),component:{message:'Hello World'}});
Component Types: Classes, Instances, or Objects
You can enhance with three different component types:
Key Enhancement Concepts
Existing DOM is preserved: Enhancement doesn't replace your HTML - it makes it interactive
const enhanceRoot = await Aurelia.enhance({
host: element,
component: {
data: null,
// Called when component is being set up
created() {
console.log('Component created');
},
// Called before data binding starts
binding() {
console.log('Starting data binding');
},
// Called after data binding completes
bound() {
console.log('Data binding complete');
},
// Called when component is being attached to DOM
attaching() {
console.log('Attaching to DOM');
},
// Called after component is attached to DOM
attached() {
console.log('Attached to DOM - ready for user interaction');
// Good place for focus, animations, etc.
},
// Called when component is being removed
detaching() {
console.log('Detaching from DOM');
},
// Called when data bindings are being torn down
unbinding() {
console.log('Unbinding data');
// Cleanup subscriptions, timers, etc.
}
}
});