如何使用Angular刷新當前頁面
這篇文章給大家介紹如何使用Angular刷新當前頁面,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
創新互聯建站專注為客戶提供全方位的互聯網綜合服務,包含不限于網站制作、成都網站設計、達日網絡推廣、微信小程序開發、達日網絡營銷、達日企業策劃、達日品牌公關、搜索引擎seo、人物專訪、企業宣傳片、企業代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創新互聯建站為所有大學生創業者提供達日建站搭建服務,24小時服務熱線:028-86922220,官方網址:www.cdcxhl.com
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; 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); } }
關于如何使用Angular刷新當前頁面就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享題目:如何使用Angular刷新當前頁面
網頁地址:http://www.xueling.net.cn/article/ppcise.html