這篇文章給大家介紹如何使用Angular刷新當(dāng)前頁面,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

onSameUrlNavigation
從angular5.1起提供onSameUrlNavigation來支持路由重新加載。
有兩個(gè)值'reload'和'ignore'。默認(rèn)為'ignore'
定義當(dāng)路由器收到一個(gè)導(dǎo)航到當(dāng)前 URL 的請求時(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)聽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);
}
}關(guān)于如何使用Angular刷新當(dāng)前頁面就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前題目:如何使用Angular刷新當(dāng)前頁面-創(chuàng)新互聯(lián)
本文URL:http://chinadenli.net/article12/dghddc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、定制開發(fā)、營銷型網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、App設(shè)計(jì)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容