新聞中心
教程:創(chuàng)建自定義路由匹配器
Angular Router 支持強大的匹配策略,你可以使用它來幫助用戶在應用中導航。該匹配策略支持靜態(tài)路由、帶參數(shù)的可變路由、通配符路由等。此外,還可以為更復雜的 URL 構建你自己的自定義模式匹配。

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于網(wǎng)站制作、做網(wǎng)站、寧波網(wǎng)絡推廣、微信平臺小程序開發(fā)、寧波網(wǎng)絡營銷、寧波企業(yè)策劃、寧波品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學生創(chuàng)業(yè)者提供寧波建站搭建服務,24小時服務熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
在本教程中,你將使用 Angular 的 ?UrlMatcher ?來構建自定義路由匹配器。此匹配器在 URL 中查找 Twitter ID。
有關本教程最終版本的工作示例,請參閱現(xiàn)場演練 / 下載范例。
目標
實現(xiàn) Angular 的 ?UrlMatcher ?以創(chuàng)建自定義路由匹配器。
創(chuàng)建一個范例應用
使用 Angular CLI,創(chuàng)建一個新應用程序 angular-custom-route-match。除了默認的 Angular 應用程序框架之外,還將創(chuàng)建一個 profile 組件。
- 創(chuàng)建一個新的 Angular 項目 angular-custom-route-match。
- 打開終端窗口,進到 ?
angular-custom-route-match? 目錄。 - 創(chuàng)建一個組件 profile。
- 在你的代碼編輯器中,找到文件 ?
profile.component.html? 并將其占位內(nèi)容替換為以下 HTML。 - 在你的代碼編輯器中,找到文件 ?
app.component.html? 并將其占位內(nèi)容替換為以下 HTML。
ng new angular-custom-route-match當提示 ?Would you like to add Angular routing?? 時,選擇 ?Y?。
當系統(tǒng)提示 ?Which stylesheet format would you like to use?? 時,選擇 ?CSS?。
片刻之后,一個新項目 ?angular-custom-route-match? 就準備好了。
ng generate component profile
Hello {{ username$ | async }}!
Routing with Custom Matching
Navigate to my profile
為你的應用程序配置路由
應用程序框架就緒后,接下來就要向 ?app.module.ts? 文件中添加路由能力。首先,你要創(chuàng)建一個自定義 URL 匹配器,用于在 URL 中查找 Twitter ID。此 ID 由其前導 ?@? 符號標識出來。
- 在你的代碼編輯器中,打開 ?
app.module.ts? 文件。 - 為 Angular 的 ?
RouterModule?和 ?UrlMatcher?添加 ?import?語句。 - 在 ?
imports?數(shù)組中,添加 ?RouterModule.forRoot([])? 語句。 - 將如下代碼添加到 ?
RouterModule.forRoot()? 語句中,以便使用自定義路由匹配器。
import { RouterModule, UrlSegment } from '@angular/router';@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
{
/* . . . */
])],
declarations: [ AppComponent, ProfileComponent ],
bootstrap: [ AppComponent ]
})matcher: (url) => {
if (url.length === 1 && url[0].path.match(/^@[\w]+$/gm)) {
return {
consumed: url,
posParams: {
username: new UrlSegment(url[0].path.slice(1), {})
}
};
}
return null;
},
component: ProfileComponent
}這個自定義匹配器是一個執(zhí)行以下任務的函數(shù):
- 匹配器驗證數(shù)組是否只包含一個區(qū)段。
- 匹配器使用正則表達式來確保用戶名的格式是匹配的。
- 如果匹配,則該函數(shù)返回整個 URL,將路由參數(shù) ?
username?定義為路徑的子字符串。 - 如果不匹配,則該函數(shù)返回 ?
null?并且路由器繼續(xù)查找與 URL 匹配的其他路由。
自定義 URL 匹配器的行為與任何其他路由定義方式是一樣的。請像定義任何其他路由一樣定義子路由或惰性加載路由。
訂閱路由參數(shù)
自定義匹配器就位后,你現(xiàn)在需要訂閱 ?profile ?組件中的路由參數(shù)。
- 在你的代碼編輯器中,打開 ?
profile.component.ts? 文件。 - 為 Angular 的 ?
ActivatedRoute?和 ?ParamMap?添加 ?import?語句。 - 為 RxJS 的 ?
map?添加 ?import?語句。 - 訂閱 ?
username?路由參數(shù)。 - 將 ?
ActivatedRoute?注入到組件的構造函數(shù)中。
import { ActivatedRoute, ParamMap } from '@angular/router';import { map } from 'rxjs/operators';username$ = this.route.paramMap
.pipe(
map((params: ParamMap) => params.get('username'))
);constructor(private route: ActivatedRoute) { }測試你的自定義 URL 匹配器
代碼就緒后,就可以測試自定義 URL 匹配器了。
- 在終端窗口中,運行 ?
ng serve? 命令。 - 打開瀏覽器訪問 ?
http://localhost:4200?。 - 單擊 my profile 超鏈接。
ng serve你會看到一個網(wǎng)頁,其中包含一個句子,內(nèi)容為 ?Navigate to my profile?。
一個新的句子 ?Hello, Angular!? 出現(xiàn)在頁面上。
分享文章:創(chuàng)新互聯(lián)Angular教程:Angular教程:創(chuàng)建自定義路由匹配器
當前網(wǎng)址:http://www.5511xx.com/article/djoioec.html


咨詢
建站咨詢
