How to create a singleton service in Aurelia?

会有一股神秘感。 提交于 2019-12-01 15:56:35
JamesCarters

Just inject it

By default, the DI container assumes that everything is a singleton instance; one instance for the app. However, you can use a registration decorator to change this.

So I realized I was thinking about this too hard. I was trying to depend on the framework (Aurelia) to do all the work, but actually it was a simple ES6 class change that makes it an instance.

let instance = null;

export class SingletonService {

	constructor() {
		if(!instance) {
			instance = this;
		}

		return instance;
	}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!