I am building an Angular 2 app with version beta.8.
In this app i have a component which implements OnInit.
In this component i have the function ngOnInit, but the ngOnInit function is never called.
import { Component, OnInit } from 'angular2/core';
@Component({
templateUrl: '/app/html/overview.html'
})
export class OverviewComponent implements OnInit {
ngOnInit() {
console.log('ngOnInit');
}
}
The Routing of the app:
@RouteConfig([
{
path: '/overview',
name: 'Overview',
component: OverviewComponent
},
{
path: '/login',
name: 'Login',
component: LoginComponent
},
{
path: '/register',
name: 'Register',
component: RegisterComponent,
useAsDefault: true
}
])
There is a check to see if the user is already logged in inside the LoginComponent and RegisterComponent.
If the user is logged in, the components redirect to Overview using: router.navigate(['Overview'])
.
If i use the Overview route as default i do see ngOnInit inside the console.
So the way i redirect my page seems to be the problem.
How can redirect to the Overview page and call the ngOnInit function?
Both the RegisterComponent
and the LoginComponent
use
ngOnInit() {
this._browser.getStorageValue('api_key', api_key => {
if (api_key) {
this._browser.gotoMain();
}
})
}
Browser is a class in which i store browser specific code. This is the gotoMain function:
gotoMain() {
this._router.navigate([this._browser.main]);
}
this._browser.main
is just a string in this case 'Overview'.
OverviewComponent
works? If yes, Even if you don't importOnInit
and implementsOnInit
,ngOnInit
will work. – Unjustmessage: string = 'test'
to the OverviewComponent and tried to display it in the view with{{message}}
and it worked. – PrognathousRegisterComponent
and inside theLoginComponent
i have a workingngOnInit()
in which i check if the user is logged in. If the user is logged in i usethis._router.navigate['Overview']
– Prognathousthis._router.navigate['Overview']
. In the constructor, a callback from a library that communicates to the server, ...? – Haterouter.navigate['Overview']
. – PrognathousngOnInit()
of myLoginComponent
this._browser.getStorageValue('api_key', api_key => { if (api_key) { this._browser.gotoMain(); } })
– Prognathousroute.config
and asGunter
has said. – Unjust..
accent in your name through keyboard. – Unjustue
,oe
instead ofü
,ö
on systems where umlauts are not available, but justu
,o
is fine as well in informal use. – Hate