国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Angular刷新當前頁面的實現方法

2024-05-06 15:27:51
字體:
來源:轉載
供稿:網友

onSameUrlNavigation

從angular5.1起提供onSameUrlNavigation來支持路由重新加載。

有兩個值'reload'和'ignore'。默認為'ignore'

定義當路由器收到一個導航到當前 URL 的請求時應該怎么做。 默認情況下,路由器將會忽略這次導航。但這樣會阻止類似于 "刷新" 按鈕的特性。 使用該選項可以配置導航到當前 URL 時的行為。

使用

配置onSameUrlNavigation

@NgModule({ imports: [RouterModule.forRoot(  routes,  { onSameUrlNavigation: 'reload' } )], exports: [RouterModule]})

reload實際上不會重新加載路由,只是重新出發掛載在路由器上的事件。

配置runGuardsAndResolvers

runGuardsAndResolvers有三個值:

paramsChange: 僅在路由參數更改時觸發。如/reports/:id 中id更改 paramsOrQueryParamsChange: 當路由參數更改或參訓參數更改時觸發。如/reports/:id/list?page=23中的id或page屬性更改 always :始終觸發
const routes: Routes = [ {  path: '',  children: [   { path: 'report-list', component: ReportListComponent },   { path: 'detail/:id', component: ReportDetailComponent, runGuardsAndResolvers: 'always' },   { path: '', redirectTo: 'report-list', pathMatch: 'full' }  ] }];

組件監聽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,避免內存泄漏  if (this.navigationSubscription) {   this.navigationSubscription.unsubscribe();  } } initLoad(e) {  window.scrollTo(0, 0);  console.log(e); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 开化县| 株洲市| 抚松县| 榆中县| 中宁县| 鞍山市| 平武县| 正镶白旗| 温泉县| 新民市| 莒南县| 平潭县| 漳平市| 大名县| 茂名市| 喀喇沁旗| 元朗区| 绍兴市| 丹棱县| 望都县| 南部县| 宜都市| 宣汉县| 揭阳市| 措勤县| 深水埗区| 海原县| 南丹县| 安福县| 姜堰市| 宝丰县| 璧山县| 木兰县| 沈丘县| 宾阳县| 陆良县| 宜川县| 涞水县| 富源县| 随州市| 乐陵市|