從angular5.1起提供onSameUrlNavigation來支持路由從新加載。、css
有兩個值'reload'和'ignore'。默認爲'ignore'html
定義當路由器收到一個導航到當前 URL 的請求時應該怎麼作。 默認狀況下,路由器將會忽略此次導航。但這樣會阻止相似於 "刷新" 按鈕的特性。 使用該選項能夠配置導航到當前 URL 時的行爲。git
@NgModule({ imports: [RouterModule.forRoot( routes, { onSameUrlNavigation: 'reload' } )], exports: [RouterModule] })
reload實際上不會從新加載路由,只是從新出發掛載在路由器上的事件。github
runGuardsAndResolvers有三個值:app
const routes: Routes = [ { path: '', children: [ { path: 'report-list', component: ReportListComponent }, { path: 'detail/:id', component: ReportDetailComponent, runGuardsAndResolvers: 'always' }, { path: '', redirectTo: 'report-list', pathMatch: 'full' } ] } ];
import {Component, OnDestroy, OnInit} from '@angular/core'; import {Observable} from 'rxjs'; import {Report} from '@models/report'; import {ReportService} from '@services/report.service'; import {ActivatedRoute, NavigationEnd, Router} from '@angular/router'; @Component({ selector: 'app-report-detail', templateUrl: './report-detail.component.html', styleUrls: ['./report-detail.component.scss'] }) export class ReportDetailComponent implements OnInit, OnDestroy { report$: Observable<Report>; navigationSubscription; constructor( private reportService: ReportService, private router: Router, private route: ActivatedRoute ) { this.navigationSubscription = this.router.events.subscribe((event: any) => { if (event instanceof NavigationEnd) { this.initLoad(event); } }); } ngOnInit() { const id = +this.route.snapshot.paramMap.get('id'); this.report$ = this.reportService.getReport(id); } ngOnDestroy(): void { // 銷燬navigationSubscription,避免內存泄漏 if (this.navigationSubscription) { this.navigationSubscription.unsubscribe(); } } initLoad(e) { window.scrollTo(0, 0); console.log(e); } }