目录
一、Angular命令创建一个配置好路由的项目
二、Angula4.x在已有的项目中配置路由
三、Angular routerLink页面跳转 默认跳转路由
四、Angular routerLinkActive设置routerLink默认选中路由
五、路由的动态传值
六、路由的js跳转
七、路由的js跳转 get传值
八、父子路由

一、Angular命令创建一个配置好路由的项目

  1. 命令创建项目

    1. ng new demo02 –-routing
  2. 创建需要的组件

  1. ng g component home
  2. ng g component news
  3. ng g component newscontent
  1. 找到app-routing.module.ts 配置路由

引入组件

  1. import { HomeComponent } from './home/home.component';
  2. import { NewsComponent } from './news/news.component';
  3. import { NewscontentComponent } from './newscontent/newscontent.component';

配置路由

  1. const routes: Routes = [
  2. {path: 'home', component: HomeComponent},
  3. {path: 'news', component: NewsComponent},
  4. {path: 'newscontent/:id', component: NewscontentComponent},
  5. { path: '', redirectTo: '/home',pathMatch: 'full' }
  6. ];
  1. 找到app.component.html根组件模板,配置router-outlet显示动态加载的路由
  1. <h1>
  2. <a routerLink="/home">首页</a>
  3. <a routerLink="/news">新闻</a>
  4. </h1>
  5. <router-outlet></router-outlet>

二、Angula4.x在已有的项目中配置路由

  1. 新建组件
  1. ng g component home
  2. ng g component news
  3. ng g component newscontent
  1. 新建app-routing.module.ts ,app-routing.module.ts中引入模块
  1. import { NgModule } from '@angular/core';
  2. import { Routes, RouterModule } from '@angular/router';
  1. app-routing.module.ts中引入组件
  1. import { HomeComponent } from './home/home.component';
  2. import { NewsComponent } from './news/news.component';
  3. import { NewscontentComponent } from './newscontent/newscontent.component';
  1. app-routing.module.ts中配置组件
  1. const routes: Routes = [
  2. {path: 'home', component: HomeComponent},
  3. {path: 'news', component: NewsComponent},
  4. {path: 'newscontent/:id', component: NewscontentComponent},
  5. { path: '', redirectTo: '/home', pathMatch: 'full' }
  6. ];
  1. app-routing.module.ts中配置模块 暴露模块
  1. @NgModule({
  2. imports: [RouterModule.forRoot(routes)],
  3. exports: [RouterModule]
  4. })
  5. export class AppRoutingModule { }
  1. 在app.module.ts 引入刚才定义的路由
  1. import { AppRoutingModule } from './app-routing.module';

6.app.module.ts里面的import注册这个路由模块

  1. imports: [ BrowserModule, AppRoutingModule ]

7.找到app.component.html根组件模板,配置router-outlet显示动态加载的路由

  1. <h1> <a routerLink="/home">首页</a> <a routerLink="/news">新闻</a> </h1>
  2. <router-outlet></router-outlet>

三、Angular routerLink页面跳转 默认跳转路由

  1. <a routerLink="/home">首页</a> <a routerLink="/news">新闻</a> //刚进来路由为空跳转的路由 { path:'', redirectTo:'
  1. //刚进来路由为空跳转的路由
  2. { path:'', redirectTo:'home', pathMatch:"full" } //匹配不到路由的时候加载的组件 或者跳转的路由
  3. { path: '**', /*任意的路由*/ // component:HomeComponent
  4. redirectTo:'home'
  5. }

四、Angular routerLinkActive设置routerLink默认选中路由

  1. <h1> <a routerLink="/home" routerLinkActive="active">首页</a> <a routerLink="/news" routerLinkActive="active">新闻</a> </h1> .active{ color:red; }

五、路由的动态传值

1.配置动态路由

  1. const routes: Routes = [
  2. {path: 'home', component: HomeComponent},
  3. {path: 'news', component: NewsComponent},
  4. {path: 'newscontent/:id', component: NewscontentComponent},
  5. { path: '', redirectTo: '/home', pathMatch: 'full' }
  6. ];

2.获取动态路由的值

  1. import { Router, ActivatedRoute, Params } from '@angular/router';
  2. constructor( private route: ActivatedRoute) {
  3. }
  4. ngOnInit() {
  5. console.log(this.route.params);//
  6. this.route.params.subscribe(data=>this.id=data.id);
  7. }

六、路由的js跳转

  1. 引入
  1. import { Router } from '@angular/router';

2.初始化

  1. export class HomeComponent implements OnInit {
  2. constructor(private router: Router) { }
  3. ngOnInit() { }
  4. goNews(){
  5. this.router.navigate(['/news', hero.id]);
  6. this.router.navigate(['/news']);
  7. }
  8. }

3.路由跳转

  1. this.router.navigate(['/news', hero.id]);

七、路由的js跳转 get传值

  1. 引入NavigationExtras
  1. import { Router ,NavigationExtras,ActivatedRoute} from '@angular/router';

2.定义一个goNewsContent方法执行跳转,用NavigationExtras配置传参。

  1. goNewsContent(){
  2. let navigationExtras: NavigationExtras = {
  3. queryParams: { 'session_id': '123' },
  4. fragment: 'anchor' };
  5. this.router.navigate(['/news'],navigationExtras);
  6. }

3.获取get传值

  1. constructor(private route: ActivatedRoute) { console.log(this.route.queryParams); }

八、父子路由

  1. 创建组件引入组件
  1. import { NewsaddComponent } from './components/newsadd/newsadd.component';
  2. import { NewslistComponent } from './components/newslist/newslist.component';
  1. 配置路由
  1. { path: 'news', component:NewsComponent,
  2. children: [
  3. { path:'newslist', component:NewslistComponent },
  4. { path:'newsadd', component:NewsaddComponent }
  5. ]
  6. }
  1. 父组件中定义router-outlet
  1. <router-outlet></router-outlet>
作者:admin  创建时间:2018-06-18 17:38
 更新时间:2018-06-18 17:58
上一篇:
下一篇: