How to inject Element in Aurelia 2

In Aurelia 1, I used to do: import { Element, inject } from 'aurelia-framework'; @inject(Element) class MyClass { constructor(element) { this.element = element } }. How do I do that in Aurelia 2 (using ESNext)? I missed that in the docs (at docs.aurelia.io). Thanks!

1 Like

Hi @frmjp! You need to use the @INode decorator. It looks as follows.

import { INode } from '@aurelia/runtime-html';

public constructor(
  @INode private readonly element: HTMLElement
){}

Note that instead of typing it as HTMLElement, you can also type it as INode. However, the HTMLElement type in this case would be more user-freindly.

1 Like

This works for me in AU2

import { inject } from 'aurelia'

@inject(Element)

class MyComponent {
    constructor(element) {
        this.element = element
    }
}
3 Likes