這篇文章給大家介紹如何使用Angular刷新當(dāng)前頁面,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、達(dá)日網(wǎng)絡(luò)推廣、微信小程序開發(fā)、達(dá)日網(wǎng)絡(luò)營銷、達(dá)日企業(yè)策劃、達(dá)日品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供達(dá)日建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:chinadenli.net
onSameUrlNavigation
從angular5.1起提供onSameUrlNavigation來支持路由重新加載。
有兩個(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)聽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)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前名稱:如何使用Angular刷新當(dāng)前頁面
瀏覽地址:http://chinadenli.net/article34/ppcise.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站營銷、小程序開發(fā)、品牌網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)