Angular resolve基礎(chǔ)用法詳解
簡(jiǎn)介
為什么使用resolve:
當(dāng)路由切換的時(shí)候,被路由的頁面中的元素(標(biāo)簽)就會(huì)立馬顯示出來,同時(shí),數(shù)據(jù)會(huì)被準(zhǔn)備好并呈現(xiàn)出來。但是注意,數(shù)據(jù)和元素并不是同步的,在沒有任何設(shè)置的情況下,AngularJS默認(rèn)先呈現(xiàn)出元素,而后再呈現(xiàn)出數(shù)據(jù)。這樣就會(huì)導(dǎo)致頁面會(huì)被渲染兩遍,導(dǎo)致“頁面UI抖動(dòng)”的問題,對(duì)用戶不太友好。resolve的出現(xiàn)解決了這個(gè)問題。
resolve是干嘛用的:
resolve屬性里的值會(huì)在路由成功前被預(yù)先設(shè)定好,然后注入到控制器中。通俗地將,就是等數(shù)據(jù)都“就位”后,才進(jìn)行路由(其實(shí)我覺得也不能叫路由,因?yàn)槁酚墒且恍┝械牟僮?,其中就包括了設(shè)置resolve屬性等等)。這樣的好處就是頁面僅會(huì)被渲染一遍。
這樣,我們就可以通過這種方式來動(dòng)態(tài)加載相應(yīng)的文件,減輕首頁加載的負(fù)擔(dān)。
一、使用場(chǎng)景
resolve保證了數(shù)據(jù)獲取后再進(jìn)行路由跳轉(zhuǎn),防止因?yàn)閿?shù)據(jù)延遲而出現(xiàn)短暫的空組件情況,以此增強(qiáng)用戶體驗(yàn)。
應(yīng)用resolve還可以進(jìn)行路由攔截,例如某些網(wǎng)站如果用戶未登錄,在跳轉(zhuǎn)到某一頁面時(shí)會(huì)提示未登錄然后強(qiáng)行回跳至前一頁面,這時(shí)如果使用resolve就可以在跳轉(zhuǎn)發(fā)生前判斷登錄狀態(tài)以決定是否允許跳轉(zhuǎn)。
二、基礎(chǔ)用法
示例中跳轉(zhuǎn)邏輯為 home.component => resolve.service => detail.component
home-routing.module.ts
import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { DetailResolver } from './detail-resolver.service'; import { DetailComponent } from './detail.component'; const routes: Routes = [ { path: ':id', component: DetailComponent, resolve: { // 此處使用resolve detail: DetailResolver } }, ]; @NgModule({ imports: [ RouterModule.forChild(routes) ], exports: [ RouterModule ], providers: [ DetailResolver ] }) export class HomeRoutingModule { }
detail-resolver.service.ts
import { Injectable } from '@angular/core'; import { Router, Resolve, RouterStateSnapshot, ActivatedRouteSnapshot } from '@angular/router'; import { Observable } from 'rxjs'; import { map, take } from 'rxjs/operators'; import { detail, DetailService } from './detail.service'; @Injectable() export class DetailResolver implements Resolve<Detail> { constructor(private detailService: DetailService, private router: Router) {} resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Detail> { let id = route.paramMap.get('id'); return this.detailService.getDetail(id).pipe( take(1), // 可選,只發(fā)出源 Observable 最初發(fā)出的的1個(gè)值 map(res => { if (res) { return res; } else { // 請(qǐng)求失敗時(shí)攔截跳轉(zhuǎn) this.router.navigate(['/home']); return null; } }) ); } }
由路由器提供的 Observable 必須完成,否則導(dǎo)航不會(huì)繼續(xù)。
detail.component.ts
// 通過 route 獲取 detail-resolver.service 中 detailService.getDetail 請(qǐng)求的數(shù)據(jù) ngOnInit() { this.route.data .subscribe((data: { detail: Detail }) => { this.detail = data.detail; }); }
參考Angular中文網(wǎng) <Resolve: 預(yù)先獲取組件數(shù)據(jù)>
API地址 <resolve守衛(wèi)>
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
AngularJS自定義插件實(shí)現(xiàn)網(wǎng)站用戶引導(dǎo)功能示例
這篇文章主要介紹了AngularJS自定義插件實(shí)現(xiàn)網(wǎng)站用戶引導(dǎo)功能,結(jié)合實(shí)例形式分析了AngularJS自定義插件的實(shí)現(xiàn)步驟與相關(guān)功能技巧,需要的朋友可以參考下2016-11-11Spartacus CMS Feature selector的實(shí)現(xiàn)解析
這篇文章主要為大家介紹了Spartacus CMS Feature selector的實(shí)現(xiàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Angularjs 創(chuàng)建可復(fù)用組件實(shí)例代碼
這篇文章主要介紹了Angularjs 創(chuàng)建可復(fù)用組件實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10詳解為生產(chǎn)環(huán)境編譯Angular2應(yīng)用的方法
這篇文章主要介紹了詳解為生產(chǎn)環(huán)境編譯Angular2應(yīng)用的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12Angular依賴注入optional?constructor?parameters概念
這篇文章主要為大家介紹了Angular?依賴注入領(lǐng)域里?optional?constructor?parameters?的概念及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11全面解析Angular中$Apply()及$Digest()的區(qū)別
$apply()和$digest()在AngularJS中是兩個(gè)核心概念,但是有時(shí)候它們又讓人困惑。這篇文章主要介紹了Angular中$Apply()及$Digest()區(qū)別詳細(xì)說明的相關(guān)資料,需要的朋友可以參考下2016-08-08