什麼是路由?這篇文章帶大家深入了解一下Angular中的路由,希望對大家有幫助!
路由簡介
#路由是實現單一頁面應用程式的一種方式,透過監聽hash或者history的變化,渲染不同的元件,起到局部更新的作用,避免每次URL變化都向伺服器請求資料。 【相關教學推薦:《angular教學》】
路由設定
設定路由模組:approuter.module. ts
const routes: Routes = [ { path: "first", component: FirstComponent }, { path: "parent", component: SecondComponent } ] @NgModule({ imports: [ CommonModule, // RouterModule.forRoot方法会返回一个模块,其中包含配置好的Router服务 // 提供者,以及路由库所需的其它提供者。 RouterModule.forRoot(routes, { // enableTracing: true, // <-- debugging purposes only // 配置所有的模块预加载,也就是懒加载的模块,在系统空闲时,把懒加载模块加载进来 // PreloadAllModules 策略不会加载被CanLoad守卫所保护的特性区。 preloadingStrategy: PreloadAllModules }) ], exports: [ FirstComponent, SecondComponent, RouterModule ], declarations: [ FirstComponent, SecondComponent ] }) export class ApprouterModule { }
app.module.ts中引入改模組:
imports: [ ApprouterModule ]
重定向路由:##
const routes: Routes = [ { path: "", redirectTo: "first", pathMatch: "full" } ]
#通配符路由:
const routes: Routes = [ // 路由器会使用先到先得的策略来选择路由。 由于通配符路由是最不具体的那个,因此务必确保它是路由配置中的最后一个路由。 { path: "**", component: NotFoundComponent } ]
路由懶載入:
設定懶載入模組可以讓首屏渲染速度更快,只有點擊懶載入路由的時候,對應的模組才會改變。const routes: Routes = [ { path: 'load', loadChildren: () => import('./load/load.module').then(m => m.ListModule), // CanLoadModule如果返回false,模块里面的子路由都没有办法访问 canLoad: [CanLoadModule] }, ]
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { LoadComponent } from './Load.component'; import { RouterModule, Routes } from '@angular/router'; import { LoadTwoComponent } from '../../../app/components/LoadTwo/LoadTwo.component'; import { LoadOneComponent } from '../../../app/components/LoadOne/LoadOne.component'; const routes: Routes = [ { path: "", component: LoadComponent, children: [ { path: "LoadOne", component: LoadOneComponent }, { path: "LoadTwo", component: LoadTwoComponent } ] }, ] @NgModule({ imports: [ CommonModule, //子模块使用forChild配置 RouterModule.forChild(routes) ], declarations: [ LoadComponent, LoadOneComponent, LoadTwoComponent ] }) export class LoadModule { }
<a [routerLink]="[ 'LoadOne' ]">LoadOne</a> <a [routerLink]="[ 'LoadTwo' ]">LoadTwo</a> <router-outlet></router-outlet>
const routes: Routes = [ { path: "second/:id", component: SecondComponent }, ]
//routerLinkActive配置路由激活时的类 <a [routerLink]="[ '/second', 12 ]" routerLinkActive="active">second</a>
import { ActivatedRoute, ParamMap, Router } from '@angular/router'; import { Component, OnInit } from '@angular/core'; import { switchMap } from 'rxjs/operators'; @Component({ selector: 'app-second', templateUrl: './second.component.html', styleUrls: ['./second.component.scss'] }) export class SecondComponent implements OnInit { constructor(private activatedRoute: ActivatedRoute, private router: Router) { } ngOnInit() { console.log(this.activatedRoute.snapshot.params); //{id: "12"} // console.log(this.activatedRoute); // 这种形式可以捕获到url输入 /second/18 然后点击<a [routerLink]="[ '/second', 12 ]">second</a> // 是可以捕获到的。上面那种是捕获不到的。因为不会触发ngOnInit,公用了一个组件实例。 this.activatedRoute.paramMap.pipe( switchMap((params: ParamMap) => { console.log(params.get('id')); return "param"; })).subscribe(() => { }) } gotoFirst() { this.router.navigate(["/first"]); } }
queryParams參數傳值,參數取得也是透過啟動的路由的依賴注入
<!-- queryParams参数传值 --> <a [routerLink]="[ '/first' ]" [queryParams]="{name: 'first'}">first</a> <!-- ts中传值 --> <!-- this.router.navigate(['/first'],{ queryParams: { name: 'first' }); -->
路由守衛:canActivate,canDeactivate,resolve,canLoad
路由守衛會回傳一個值,如果回傳true繼續執行,false阻止該行為,UrlTree導航到新的路由。 路由守衛可能會導航到其他的路由,這時候應該會回傳false。路由守衛可能會根據伺服器的值來 決定是否進行導航,所以還可以回到Promise或 Observable,路由會等待 傳回的值是true還是false。 canActivate導航到某路由。 canActivateChild導航到某子路由。const routes: Routes = [ { path: "parent", component: ParentComponent, canActivate: [AuthGuard], children: [ // 无组件子路由 { path: "", canActivateChild: [AuthGuardChild], children: [ { path: "childOne", component: ChildOneComponent }, { path: "childTwo", component: ChildTwoComponent } ] } ], // 有组件子路由 // children: [ // { path: "childOne", component: ChildOneComponent }, // { path: "childTwo", component: ChildTwoComponent } // ] } ]
import { Injectable } from '@angular/core'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; @Injectable({ providedIn: 'root', }) export class AuthGuard implements CanActivate { canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot): any { // return true; // 返回Promise的情况 return new Promise((resolve,reject) => { setTimeout(() => { resolve(true); }, 3000); }) } }
import { Injectable } from '@angular/core'; import { ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router'; @Injectable({ providedIn: 'root', }) export class AuthGuardChild implements CanActivateChild { constructor() {} canActivateChild( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { return true; } }
<!-- 使用相对路径 --> <a [routerLink]="[ './childOne' ]">one</a> <!-- 使用绝对路径 --> <a [routerLink]="[ '/parent/childTwo' ]">two</a> <router-outlet></router-outlet>
const routes: Routes = [ { path: "first", component: FirstComponent, canDeactivate: [CanDeactivateGuard] } ]
import { FirstComponent } from './components/first/first.component'; import { RouterStateSnapshot } from '@angular/router'; import { ActivatedRouteSnapshot } from '@angular/router'; import { Injectable } from '@angular/core'; import { CanDeactivate } from '@angular/router'; @Injectable({ providedIn: 'root', }) export class CanDeactivateGuard implements CanDeactivate<any> { canDeactivate( component: any, route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): boolean { // component获取到组件实例 console.log(component.isLogin); return true; } }
const routes: Routes = [ { path: 'load', loadChildren: () => import('./load/load.module').then(m => m.LoadModule), // CanLoadModule如果返回false,模块里面的子路由都没有办法访问 canLoad: [CanLoadModule] } ]
import { Route } from '@angular/compiler/src/core'; import { Injectable } from '@angular/core'; import { CanLoad } from '@angular/router'; @Injectable({ providedIn: 'root', }) export class CanLoadModule implements CanLoad { canLoad(route: Route): boolean { return true; } }
const routes: Routes = [ { path: "resolve", component: ResolveDemoComponent, resolve: {detail: DetailResolver} ]
import { Injectable } from '@angular/core'; import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; @Injectable({ providedIn: 'root' }) export class DetailResolver implements Resolve<any> { constructor() { } resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): any { return new Promise((resolve,reject) => { setTimeout(() => { resolve("resolve data"); }, 3000); }) } }
constructor(private route: ActivatedRoute) { } ngOnInit() { const detail = this.route.snapshot.data.detail; console.log(detail); }
constructor(private router: Router) { this.router.events.subscribe((event) => { // NavigationEnd,NavigationCancel,NavigationError,RoutesRecognized if (event instanceof NavigationStart) { console.log("NavigationStart"); } }) }
以上是深入了解Angular中的路由的詳細內容。更多資訊請關注PHP中文網其他相關文章!