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

首頁 > 編程 > JavaScript > 正文

Angular刷新當(dāng)前頁面的實(shí)現(xiàn)方法

2019-11-19 12:29:46
字體:
供稿:網(wǎng)友

onSameUrlNavigation

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

有兩個(gè)值'reload'和'ignore'。默認(rèn)為'ignore'

定義當(dāng)路由器收到一個(gè)導(dǎo)航到當(dāng)前 URL 的請求時(shí)應(yīng)該怎么做。 默認(rèn)情況下,路由器將會忽略這次導(dǎo)航。但這樣會阻止類似于 "刷新" 按鈕的特性。 使用該選項(xiàng)可以配置導(dǎo)航到當(dāng)前 URL 時(shí)的行為。

使用

配置onSameUrlNavigation

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

reload實(shí)際上不會重新加載路由,只是重新出發(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); }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 独山县| 天门市| 鲁甸县| 平武县| 宁河县| 汨罗市| 漠河县| 上栗县| 阳西县| 白朗县| 子洲县| 图片| 伊宁市| 五寨县| 兴隆县| 彰化市| 吉安市| 和林格尔县| 邻水| 六安市| 丘北县| 磴口县| 曲沃县| 建湖县| 威远县| 湛江市| 井研县| 铁岭市| 盖州市| 马边| 崇信县| 上林县| 休宁县| 曲阳县| 鄯善县| 云霄县| 沂南县| 上蔡县| 阳东县| 霍山县| 鄯善县|