Angular刷新當(dāng)前頁(yè)面的實(shí)現(xiàn)方法
onSameUrlNavigation
從angular5.1起提供onSameUrlNavigation來(lái)支持路由重新加載。
有兩個(gè)值'reload'和'ignore'。默認(rèn)為'ignore'
定義當(dāng)路由器收到一個(gè)導(dǎo)航到當(dāng)前 URL 的請(qǐng)求時(shí)應(yīng)該怎么做。 默認(rèn)情況下,路由器將會(huì)忽略這次導(dǎo)航。但這樣會(huì)阻止類似于 "刷新" 按鈕的特性。 使用該選項(xiàng)可以配置導(dǎo)航到當(dāng)前 URL 時(shí)的行為。
使用
配置onSameUrlNavigation
@NgModule({
imports: [RouterModule.forRoot(
routes,
{ onSameUrlNavigation: 'reload' }
)],
exports: [RouterModule]
})
reload實(shí)際上不會(huì)重新加載路由,只是重新出發(fā)掛載在路由器上的事件。
配置runGuardsAndResolvers
runGuardsAndResolvers有三個(gè)值:
- paramsChange: 僅在路由參數(shù)更改時(shí)觸發(fā)。如/reports/:id 中id更改
- paramsOrQueryParamsChange: 當(dāng)路由參數(shù)更改或參訓(xùn)參數(shù)更改時(shí)觸發(fā)。如/reports/:id/list?page=23中的id或page屬性更改
- always :始終觸發(fā)
const routes: Routes = [
{
path: '',
children: [
{ path: 'report-list', component: ReportListComponent },
{ path: 'detail/:id', component: ReportDetailComponent, runGuardsAndResolvers: 'always' },
{ path: '', redirectTo: 'report-list', pathMatch: 'full' }
]
}
];
組件監(jiān)聽(tīng)router.events
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,避免內(nèi)存泄漏
if (this.navigationSubscription) {
this.navigationSubscription.unsubscribe();
}
}
initLoad(e) {
window.scrollTo(0, 0);
console.log(e);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angularjs 創(chuàng)建可復(fù)用組件實(shí)例代碼
這篇文章主要介紹了Angularjs 創(chuàng)建可復(fù)用組件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10
AngularJS實(shí)現(xiàn)單獨(dú)作用域內(nèi)的數(shù)據(jù)操作
這篇文章給大家介紹了利用AngularJs如何實(shí)現(xiàn)ng-repeat內(nèi)各個(gè)小的子作用域單獨(dú)數(shù)據(jù)綁定。有需要的小伙伴們可以參考借鑒,下面來(lái)一起看看吧。2016-09-09
Angular中innerHTML標(biāo)簽的樣式不起作用的原因解析
這篇文章主要介紹了Angular中innerHTML標(biāo)簽的樣式不起作用詳解 ,本文給出了解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
AngularJS中的路由使用及實(shí)現(xiàn)代碼
本篇文章主要介紹了AngularJS中的路由使用及實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
詳解Angular-cli生成組件修改css成less或sass的實(shí)例
這篇文章主要介紹了詳解Angular-cli生成組件修改css成less或sass的實(shí)例的相關(guān)資料,這里主要講解修改angular-cli.json文件,生成css或者less,需要的朋友可以參考下2017-07-07

