# How to get singleton component (Aurelia 2)

**URL:** <https://discourse.aurelia.io/t/how-to-get-singleton-component-aurelia-2/5113>\
**Category:** Uncategorized\
**Created:** [March 9, 2023, 3:47pm UTC](https://discourse.aurelia.io/t/how-to-get-singleton-component-aurelia-2/5113 "2023-03-09T15:47:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![frmjp](https://avatars.discourse-cdn.com/v4/letter/f/b9e5f3/32.png) [@frmjp](https://discourse.aurelia.io/u/frmjp)\
**Post date:** [March 9, 2023, 3:47pm UTC](https://discourse.aurelia.io/t/how-to-get-singleton-component-aurelia-2/5113/1 "2023-03-09T15:47:27Z")

</div>

Hi! I encapsulate my Aurelia 2 app in a web component (see below). `MyApp` component should be a singleton, and I would like to invoke one of its methods later on, thus I try to get it from the app’s container, but I end up getting a new instance that gets created.

```auto
import Aurelia from 'aurelia';
import { MyApp } from './my-app';

export class MyWebComponent extends HTMLElement {
  connectedCallback() {
    this.app = Aurelia.app({
      component: MyApp,
      host: document.querySelector('my-web-component'),
    });
    this.app.start();
  }

  myMethod() {
    this.app.container.get(MyApp).aMethod();
  }
}

customElements.define('my-web-component', MyWebComponent);

```

Please let me know if I got something wrong. I feel that the docs don’t help a lot on that.

Please consider that I am using ESNext.

Thanks!

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [March 9, 2023, 5:56pm UTC](https://discourse.aurelia.io/t/how-to-get-singleton-component-aurelia-2/5113/2 "2023-03-09T17:56:44Z")

</div>

Hi @frmjp! As the `MyApp` is your app root, you don’t need to use the container to grab the `MyApp` instance.

Instead, you can do this:

```typescript
import { ICustomElementController } from '@aurelia/runtime-html';

(this.app.root.controller as ICustomElementController<MyApp>)
  .viewModel
  .aMethod()

```

Note that you can also explicitly register a singleton component. Refer the [docs](https://docs.aurelia.io/getting-to-know-aurelia/dependency-injection-di#registering-services) for details.
