How to create a singleton service in Aurelia?

前端 未结 2 1982
忘了有多久
忘了有多久 2021-01-17 10:11

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

相关标签:
2条回答
  • 2021-01-17 10:50

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

    0 讨论(0)
  • 2021-01-17 11:08

    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.

    0 讨论(0)
提交回复
热议问题