おはようございます。角度2 ngOnDestroyがトリガーされていません
私はルートを持った小さなangle2-client-appを開発しました。
app.routes.ts今私のクライアントがコンポーネントへコンポーネントから迂回できるよう
import { provideRouter, RouterConfig } from '@angular/router';
import { ContactComponent } from './components/contact.component/contact.component';
import { Home } from './components/home.component/home.component';
import {StudentRoutes} from './components/student.component/student.routes';
import {LecturerRoutes} from "./components/lecturer.component/lecturer.routes";
import {ProcessRoutes} from "./components/process.component/process.routes";
import {SchoolRoutes} from "./components/school.component/school.routes";
export const routes: RouterConfig = [
{ path: '', component: Home },
...ProcessRoutes,
...StudentRoutes,
...LecturerRoutes,
...SchoolRoutes,
{ path: 'contact', component: ContactComponent }
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];
:ルートは、次のように作成されます。
私のすべてのコンポーネントは1つの親コンポーネントを拡張している:ベースcomponent.ts
をベースcomponent.ts
import { Component, Injectable, Inject, Input, Output, EventEmitter, OnDestroy, OnInit, OnChanges } from '@angular/core';
import { Http, URLSearchParams, Headers, Response, RequestOptions } from '@angular/http';
import { NavigationService } from '../services/navigation-service';
// import {Router } from '@angular/router';
import { ComponentService } from '../services/component-service';
import { MenuItem } from '../models/menu-item';
import { Subscription } from 'rxjs/Subscription';
import {Broadcaster} from "../services/broadcaster";
import {ScreenSize} from "../models/screen-size";
import {LoaderService} from "../services/loader-service";
@Component({
selector: 'base-component',
template: ""
})
@Injectable()
export class BaseComponent implements OnDestroy, OnInit {
constructor(componentService: ComponentService,
protected navigationService: NavigationService,
broadcaster: Broadcaster,
protected loaderSrv:LoaderService,
screen:ScreenSize = ScreenSize.TwoColumns) {
let items = new Array<MenuItem>();
let parent = componentService.GetParentPath(location.pathname);
this.navigationService.GetSideNavigation(parent).subscribe((x) => {
items = x.map(y => new MenuItem(y.name, y.url, y.children))
broadcaster.broadcast("sideNavigation", items[0].children);
});
broadcaster.broadcast("screenSize", screen);
}
ngOnDestroy(){
this.loaderSrv.start();
}
ngOnInit(){
this.loaderSrv.stop();
}
}
たびに、ユーザーがルートを変更し、彼が最初にすべきngOnDestroyメソッドを押してください。オブジェクトがクリアされるためです。その後は、ngOnInitメソッドにヒットする必要があります。これは、ルーティングすることで新しいオブジェクトを作成したばかりなので、メソッドです。
問題: それは、ngOnDestroyメソッドをトリガーすることはありません...彼はngOnInit-メソッドをトリガーませんが、決してngOnDestroyん...あなたができるので、ここで
は私のpackage.jsonのうちの一部です私のangular2のバージョンを確認してください:
"@angular/common": "2.0.0-rc.3",
"@angular/compiler": "2.0.0-rc.3",
"@angular/core": "2.0.0-rc.3",
"@angular/forms": "0.1.1",
"@angular/http": "2.0.0-rc.3",
"@angular/platform-browser": "2.0.0-rc.3",
"@angular/platform-browser-dynamic": "2.0.0-rc.3",
"@angular/router": "3.0.0-alpha.7",
"@angular/router-deprecated": "2.0.0-rc.2",
"@angular/upgrade": "2.0.0-rc.3"
誰かが私に良いヒントを与えることができれば素晴らしいでしょう。
ピースアウト!
あるページから別のページへナビゲートするときに同じページを破棄するこのページのチェックは、ナビゲート中にデリートできるものです – mayur
あなたのコメントが正しい場合は、2つのメソッドを1つのコンポーネント?私はまた、それも働いていないことを確認します。 ngOnDestroyはコンポーネント自体でもトリガされません。私はあなたに正しいことがない場合、別のコメントを残してください! –
チェック詳細はhttps://angular.io/docs/ts/latest/api/core/index/OnDestroy-class.htmlのplnkrサンプルを入手しました。 – mayur