我有以下組件,一個主要和另一個。我想從主編程導航到另一個。angular2 routerOnActivate不被調用
主要
/// <reference path="../../node_modules/angular2/typings/browser.d.ts" />
/// <reference path="../../node_modules/angular2/core.d.ts" />
/// <reference path="../../node_modules/angular2/router.d.ts" />
import {Component, provide} from 'angular2/core';
import {RouteConfig, Router, ROUTER_PROVIDERS, ROUTER_DIRECTIVES} from 'angular2/router';
@RouteConfig([
{ path: '/Main/...', name: 'Main', component: Main}
])
@Component({
directives: [ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS],
selector: 'Main',
template:
'<div>
<button type="button" (click)="navigate()">Navigate</button>
</div>'
})
export class Main {
constructor(private _router: Router) { }
navigate() {
this._router.navigateByUrl('Another');
}
}
另一個
/// <reference path="../../node_modules/angular2/typings/browser.d.ts" />
/// <reference path="../../node_modules/angular2/core.d.ts" />
/// <reference path="../../node_modules/angular2/router.d.ts" />
import {Component, provide} from 'angular2/core';
import {RouteConfig, Router, ComponentInstruction, OnActivate, ROUTER_PROVIDERS, ROUTER_DIRECTIVES} from 'angular2/router';
@RouteConfig([
{ path: '/Another/...', name: 'Another', component: Another}
])
@Component({
directives: [ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS],
selector: 'Another',
template:
'<div [style.display]="isActive()">
<h1>i'm another!</h1>
</div>'
})
export class Another implements OnActivate {
constructor(private _router: Router) { }
IsActive = false;
isActive() {
return (this.IsActive === true) ? 'block' : 'none';
}
routerOnActivate(next: ComponentInstruction, prev: ComponentInstruction) {
this.IsActive = true;
console.log("called! surprisingly.");
}
}
似乎routerOnActivate
是從來沒有所謂的,雖然在瀏覽器的URL顯示Main/Another
。爲什麼不叫它?
也許polyfills設置不正確https://github.com/angular/angular/issues/8012 –
它已正確註冊在頁面上。順序與angular2快速入門建議的順序相同。沒有錯誤在控制檯中可見,所以我假設一切正常加載。 – RoninCoder
你看到'我是另一個!'當你瀏覽時,它可能無法正確導航,或者嘗試使用'router.navigate'而不是'router.navigateByUrl'導航 –