This is just me trying to get an understanding of the lifecycle and try and perform some logic before loading the component so I found the CanActivate
Annotation.
I have a function in the Component that I want to call so I need the Component; I have injected it... it seems overly complex.
// HomeComponent Component
@Component({
selector: 'HomeComponent',
template: '<h2>HomeComponent Us</h2>'
})
@CanActivate((next,prev) => {
let injector: any = Injector.resolveAndCreate([HomeComponent]);
let theComponent: HomeComponent = injector.get(HomeComponent);
return theComponent.canActivate(next,prev)
})
class HomeComponent {
data = {}
myresolver: any
constructor() {
console.log("in constructor", this.data)
}
setData(data: any) {
this.data = data
}
canActivate(nextInstr, currInstr) {
console.log("in my resolver");
var that = this;
return new Promise(function(resolve, reject) {
setTimeout(function() {
var data = { data: "Aaron" };
that.setData(data)
resolve(data);
}, 2000)
});
}
}