aurelia / framework

The Aurelia 1 framework entry point, bringing together all the required sub-modules of Aurelia.
MIT License
11.75k stars 623 forks source link

Aurelia Framework - How to manually create a singleton instance shared across view models #815

Closed andy1547 closed 7 years ago

andy1547 commented 7 years ago

Sorry for posting here but I was unable to get a response to this question on Glitter.

I'm using container.registerInstance('MyApiClient', new MyApiClient('url-not-known-until-runtime')); in main.configure to register a global singleton which my view models can share. I'm able to inject this in my viewmodels but its constructed with no parameters, not the one registered. I've tried using both @autoinject and @inject(MyApiClient) annotations.

In short, I'm simply after a way to create a singleton instance of an object at boot time and share it across my components, how do I do this? FYI I'm using TypeScript.

andy1547 commented 7 years ago

Had to supply the Type instead of the Type string i.e. aurelia.container.registerSingleton(Test, () => new Test('hello'));