In Angular 1 my config looks like this:
$routeProvider
.when("/news", {
templateUrl: "newsView.html",
controller: "newsController",
resolve: {
message: function(messageService){
return messageService.getMessage();
}
}
})
How to use resolve in Angular2?
As alexpods has already mentioned, there doesn't seem to be a 'resolve' as such. The idea seems to be that you make use of the lifecycle hooks that the router provides. These are:
Then there is @CanActivate. This is a special hook because it is called before your component is instantiated. Its parameters are (next, previous)
which are the components you're routing to and the component you've come from (or null if you have no history) respectively.
import {Component} from '@angular/core';
import {ROUTER_DIRECTIVES, CanActivate, OnActivate} from '@angular/router';
@Component({
selector: 'news',
templateUrl: 'newsView.html',
directives: [ROUTER_DIRECTIVES]
})
@CanActivate((next) => {
return messageService.getMessage()
.then((message) => {
next.params.message = message;
return true; //truthy lets route continue, false stops routing
});
})
export class Accounts implements OnActivate {
accounts:Object;
onActivate(next) {
this.message = next.params.message;
}
}
The thing I have not figured out yet is how to get the result of the promise into your onActivate - other than storing it on your 'next' component. This is because onActivate is also only invoked with next and previous and not the result of the promise. I'm not happy with that solution but it's the best I could come up with.