angular 路由笔记

  • 一、静态路由
    app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CrisisListComponent} from './crisis-list/crisis-list.component';
import { HeroDetailComponent} from './hero-detail/hero-detail.component';

const routes: Routes = [
  {path: 'crisis-center', component: CrisisListComponent},
  {path: 'hero', component: HeroDetailComponent},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
<a [routerLink]="/hero" routerLinkActive="active">Heroes</a> 
  • 二、动态路由传参
  1. 模板简单传参
    app-routing.module.ts
const routes: Routes = [
  {path: 'crisis-center', component: CrisisListComponent},
  {path: 'hero/:id', component: HeroDetailComponent},
];

app.component.html

<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a> 
<a [routerLink]="['/hero', '1']" routerLinkActive="active">Heroes</a> 

hero-detail.component.ts

const id = this.route.snapshot.paramMap.get('id');
console.log(id);
  1. 模板传递对象
    app-routing.module.ts
const routes: Routes = [
   {path: 'crisis-center', component: CrisisListComponent},
   {path: 'hero', component: HeroDetailComponent},
];

app.component.html

<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
<a [routerLink]="['/hero']" [queryParams]="{id: 1, name: 'name'}" routerLinkActive="active">Heroes</a>

hero-detail.component.ts

this.route.queryParamMap.subscribe((params: Params) => {
      console.log(params.params)
})
  1. Router的navigate方法传参
    app-routing.module.ts
const routes: Routes = [
   {path: 'crisis-center', component: CrisisListComponent},
   {path: 'hero', component: HeroDetailComponent},
];

app.component.html

<nav>
    <a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
    <button (click)="heroClick()">heroes</button> 
</nav>

app.component.ts

heroClick() {
    this.router.navigate(['/hero', {id: 1, name: 'name'}])
  }

hero-detail.component.ts

this.route.paramMap.subscribe((p: Params) => {
      console.log(p.params)
    })
  • 三、子路由
    app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ProductComponent } from './product/product.component';
import { SettingComponent } from './setting/setting.component';
import { ListComponent } from './product/list/list.component';
import { DetailComponent } from './product/detail/detail.component';

const routes: Routes = [
  {
    path: 'product', 
    component: ProductComponent,
    children: [
      {path: 'list', component: ListComponent},
      {path: 'detail', component: DetailComponent}
    ]
  },
  {path: 'setting', component: SettingComponent},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

app.component.html

<a routerLink="/product">商品</a> 
<a routerLink="/setting">设置</a>

<router-outlet></router-outlet>

product.component.html

<div>
  <h3>商品管理页面</h3>
  <a routerLink="./list">列表</a> &nbsp;
  <a routerLink="./detail">详情</a>
</div>

<router-outlet></router-outlet>
  • 三、路由守卫
    参考@llaaakk博文地址
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • summer最近越来越唠叨了,每天出门会不停的告诉我路上小心,不要被石头绊倒,不要用石头砸人,不要把人家头砸破。应...
    莫莫之言阅读 222评论 0 0
  • 2017.11.23 星期四 晴 每天都一样忙碌,可只要心情好便一切都好。今晚做了孩子们爱吃的饭菜,几乎每个盘...
    厦门路小学邵艺馨妈妈阅读 157评论 0 3
  • Excel排序和筛选有很多应用场景,是我们经常用到的功能。但,它的很多基础的用法你是不是不一定都会用?而且,排序也...
    萤火虫的秘密阅读 511评论 0 4
  • 基本信息 中文名称:张国栋毕业院校:西安冶金建筑学院 出生日期:1963年12月代表作品:各类工程造价书籍 专家介...
    栋梁之才阅读 1,364评论 0 4