How to create a singleton service in Aurelia?

 ̄綄美尐妖づ 提交于 2019-12-19 12:45:13

问题


I'm pretty new to Aurelia (only been using it a few days) and I love it!

I know how to make a service with Aurelia, but how can I make that service a singleton that I can then share data with between multiple ViewModels?

Thanks


回答1:


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.




回答2:


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;
	}
}


来源:https://stackoverflow.com/questions/31486384/how-to-create-a-singleton-service-in-aurelia

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