Cheat Sheet
Cheat Sheet
A quick reference for different aspects of Aurelia with minimal explanation.
Bootstrapping
Simple
src/main.ts
import au from 'aurelia';
import { AppRoot } from './app-root';
await au.app({
component: AppRoot,
host: document.querySelector('app-root'),
}).start();Script tag (Vanilla JS)
Note: you can copy-paste the markup below into an html file and open it directly in the browser. There is no need for any tooling for this example.
index.html
Script tag (Vanilla JS - enhance)
Note: you can copy-paste the markup below into an html file and open it directly in the browser. There is no need for any tooling for this example.
index.html
Multi-root
Note: the sample below mainly demonstrates stopping an existing instance of Aurelia and starting a new one with a different root and host. Do not consider this a complete example of a proper way to separate a private app root from public view, which is a topic of its own.
src/main.ts
src/login-wall.ts
Advanced (low-level)
When you need more control over the wireup and/or want to override some of the defaults wrapped by the 'aurelia' package and/or maximize tree-shaking of unused parts of the framework:
Custom elements
With conventions
Without conventions
Vanilla JS
Custom attributes
With conventions
Without conventions
Vanilla JS
Template controllers
With conventions
Without conventions
Vanilla JS
Binding behaviors
With conventions
Without conventions
Vanilla JS
Value converters
With conventions
Without conventions
Vanilla JS
Attribute patterns
Binding commands / instruction renderer
Templating syntax
Built-in custom attributes & template controllers (AKA directives)
Lifecycle hooks
Migrating from v1
Rename
bindtobindingIf you had timing issues in
bindand used thequeueMicroTaskto add some delay (or usedattachedfor things that really should be inbind), you could try usingboundinstead (and remove thequeueMicroTask). This hook was added to address some edge cases where you need information that's not yet available inbind, such asfrom-viewbindings andrefs.If you used
CompositionTransactionin thebindhook to await async work, you can remove that and simply return a promise (or useasync/await) inbindinginstead. The promise will be awaited by the framework before rendering the component or binding and of its children.
Rename
attachedtoattachingIf you had timing issues in
attachedand usedqueueMicroTaskor evenqueueTaskto add some delay, you can probably remove thequeueMicroTask/queueTaskand keep your logic in theattachedhook. Whereattachingis the new "called as soon as this thing is added to the DOM",attachednow runs much later than it did in v1 and guarantees that all child components have been attached as well.
Rename
unbindtounbinding(there is nounbound)Rename
detachedtodetaching(there is no moredetached)If you really need to run logic after the component is removed from the DOM, use
unbindinginstead.
If you need the
owningView, consider the interface shown below: what was "view" in v1 is now called "controller", and what was called "owningView" in v1 is now called "parentController" (or simply parent in this case). You can inject it via DI with the@IControllerdecorator /IControllerinterface, therefore it's no longer passed-in as an argument tocreated.
The view model interfaces
You can import and implement these in your components as a type-checking aid, but this is optional.
Dependency Injection
Migrating from v1
Most stuff from v1 will still work as-is, but we do recommend that you consider using DI.createInterface liberally to create injection tokens, both within your app as well as when authoring plugins.
Consumers can use these as either parameter decorators or as direct values to .get(...) / static inject = [...].
The benefit of parameter decorators is that they also work in Vanilla JS with babel and will work natively in browsers (without any tooling) once they implement them.
They are, therefore, generally the more forward-compatible and consumer-friendly option.
Creating an interface
Note: this is a multi-purpose "injection token" that can be used as a plain value (also in VanillaJS) or as a parameter decorator (in TypeScript only)
Strongly-typed with default
Useful when you want the parameter decorator and don't need the interface itself.
No default (more loosely coupled)
Injecting an interface
Registration types
Creating resolvers explicitly
This is more loosely coupled (keys can be declared independently of their implementations) but results in more boilerplate. More typical for plugins that want to allow effective tree-shaking, and less typical in apps.
These can be provided directly to e.g. au.register(dep1, dep2) as global dependencies (available in all components) or to the static dependencies = [dep1, dep1] of components as local dependencies.
Decorating classes
Customizing injection
Using lifecycle hooks in a non-blocking fashion but keeping things awaitable
Example that blocks rendering (but is simplest to develop)
Example that does not block rendering and avoids race conditions (without task queue)
Example that does not block rendering and avoids race conditions (with task queue)
Apply the practices above consistently, and you'll reap the benefits:
In the future, time-slicing will be enabled via these TaskQueue APIs as well, which will allow you to easily chunk work that's been dispatched via the task queues.
Integration (plugins, shared components, etc)
Migrating from v1
One of the biggest differences compared to Aurelia v1 is the way integrations work.
In v1, you would have a configure function like so:
index.ts (producer)
Which would then be consumed as either a plugin or a feature like so:
main.ts (consumer)
consumer
In v2 the string-based conventions are no longer a thing. We use native ES modules now. And there are no more different APIs for resources, plugins and features. Instead, everything is a dependency that can be registered to a container, its behavior may be altered by specific metadata that's added by framework decorators.
The most literal translation from v1 to v2 of the above, would be as follows:
index.ts (producer)
main.ts (consumer)
consumer
The register method
register methodIn Aurelia v2, everything (including the framework itself) is glued together via DI. The concept is largely the same whether you're building a plugin, a shared component or a service class.
The producer (or the exporting side) exposes an object with a register method, and the consumer (the importing side) passes that object into its au.register call (for global registration) or into the dependencies array of a custom element (for local registration).
The DI container calls that register method and passes itself in as the only argument. The producer can then register resources / components / tasks to that container. Internally, things like resources and tasks have special metadata associated with them which allows the framework to discover and consume them at the appropriate times.
Below are some examples of how integrations can be produced and consumed:
1.1 Simple object literal with a register method
index.ts (producer)
main.ts (consumer)
1.2 A function that returns an object literal with a register method (to pass in e.g. plugin options)
index.ts (producer)
main.ts (consumer)
1.3 An interface
index.ts (producer)
Interfaces and classes do not need to be registered explicitly. They can immediately be injected. The container will "jit register" them the first time they are requested.
1.4 A class (typically a resource)
index.ts (producer)
main.ts (consumer)
To register it as a global resource (available in all components)
OR:
name-list.ts (consumer)
To register it as a local resource (available only in that specific custom element)
1.5 A (module-like) object with any of the above as its properties
resources/index.ts (producer)
main.ts (consumer)
Last updated
Was this helpful?