Skip to content Skip to sidebar Skip to footer

Global Application State In Aurelia

I'm trying to inject a class to other places in my Aurelia app to share authentication state after login. I'm following this example http://hobbit-on-aurelia.net/appstate/ but it l

Solution 1:

If you assume that the user session is a singleton, then that is the problem. In your example the user session is a view template, which is not a singleton. Those get created (in the current implementation, this may change with caching later) whenever you navigate to a view. They also get destroyed whenever you navigate from the view.

What you want is a standalone class that you inject into the constructor of your view model.

exportclassMyViewModel {
   static inject = [UserSession];
   constructor(userSession) {
       this.userSession = userSession;
   }
}

This will create a singleton instance, the default behavior, of the service class UserSession. The container will then inject it into the view model when the view is created.

Post a Comment for "Global Application State In Aurelia"