Within an Aurelia project I have a view model that I want to maintain state between router navigation. I thought that adding @singleton()
to my view model class would accomplish this.
In fact, I have created a simple Aurelia project where this works. I am able to navigate away from and back to the same page and state is maintained. My constructor is only called the first time I navigate to that page.
import { singleton } from 'aurelia-framework';
@singleton()
export class Welcome {
heading = 'Welcome to the Aurelia Navigation App!';
constructor() {
console.log('constructor');
}
activate() {
console.log('activate');
}
attached() {
console.log('attached');
}
}
However, in my larger application this is not working. I add the decorator and my view model's constructor is still called the second time I navigate to that page. (I have even copied this view model into my larger application and it is not treated as a singleton.)
Obviously something must be different between these two projects. However, I don't see any difference. Is there a setting I may have set that would override the behavior of @singleton()
?