angular4学习笔记整理(二)angular4的路由使用

96 

这章说一下angular的路由javascript

先说angular路由怎么引入,一开始new出来的angular项目它路由帮你配好了,但看要看app.module.ts里面

1.首先最上面要引入路由模块html

import {RouterModule, Routes} from '@angular/router';

 

2.而后在ngModule里面加点东西

 
 

3.这个routeConfig须要本身定义,类型Routes,里面就是angular路由配置java

const routeConfig: Routes = [
  {path: '' , component : HomeComponent},
]

 

固然路由配置也是能够抽出来的数组

路由配置简单介绍

1.经常使用跳转app

const routeConfig: Routes = [
  {path: '' , component : HomeComponent}, //path为''首页便是
  {path: 'chat',component: ChatComponent,},//访问首页地址+/chat    即能访问到chat组件
  {path: 'au/:id',component: AuComponent},//路由param传参
  {path: '**',component: Code404Component} //不能匹配的路由由 ** 匹配
]

 

其中第三个路由传参的接收方组件想要拿到参数就须要多加点
1.首先引入dom

import {ActivatedRoute, Params} from '@angular/router';

 

2.并在constructor里注入这个路由服务ide

constructor(private routeInfo: ActivatedRoute) { }

 

3.获取param参数

其中有2种方式获取param
第一种是snapshot 参数快照this

ngOnInit() {
    //constructor建立时会只建立一次ngOnInit,因此this.routeInfo.snapshot.params['id']的值会不变
    this.productId = this.routeInfo.snapshot.params['id'];
  }

 

但有一个问题
若是已经请求 localhost:4200/au/6 后,再请求 localhost:4200/au/8 ,至关于同路由跳转只是参数不一样,那么第二次拿到的param数字仍是第一次的6
缘由spa

constructor建立时会只建立一次ngOnInit,因此this.routeInfo.snapshot.params['id']的值会不变.net

因此更多的获取参数更推荐第二种参数订阅的方式

ngOnInit() {
    this.routeInfo.params.subscribe((params: Params) => this.productId = params.id);
}

 

怎么跳参数都是对的用第二种

子路由

只是一层路由明显知足不了开发需求,能够再配置子路由

 {
    path: 'product',
    component: ProductComponent,
    children: [
      {
        path: 'childA', component: ChildAComponent
      },
      {
        path: 'childB', component: ChildBComponent
      }
    ]
  }

 

可是子路由光这样仍是不行·
在父组件html里加跳到子路由的按钮时

<a [routerLink]="['./childA']" >销售员A</a>
<a [routerLink]="['./childB']" >销售员B</a>

 

注意这里不能加/ 由于斜杠指向根路径, ./才指向相对路径

重定向路由

使用 redirectTo

const routes: Routes = [{
  path: '',
  redirectTo: 'home/6',
  pathMatch: 'full' 
}]

 

辅助路由

就是一个插座,辅助路由经过不一样的outlet配置,让页面的router-outlet标签,显示不一样内容
路由配置

const routes: Routes = [{//辅助路由指向ChatComponent组件,插座名称aux
  path: 'chat',
  component: ChatComponent,
  outlet: 'aux'
}]

 

引用插座的html代码

<a [routerLink]="[{outlets:{primary:'home/2',aux:'chat'}}]" ></a>
<a [routerLink]="[{outlets:{aux:null}}]" ></a><!--不引用辅助路由-->
<router-outlet></router-outlet> <!--插件内容显示的地方-->

 

应该会有人问第一行的primary干吗的
辅助路由的改变只会改变插座的内容,不影响主路由
好比本来路径是
http://localhost:4200/home/0
如今若是[routerLink]="[{outlets:{aux:'chat'}}]"的a标签被点击,改变的只是辅助路由,路径会变为
http://localhost:4200/home/0(aux:chat)
只有加上primary:'home/2',主路由才会一块儿变,变成http://localhost:4200/home/2(aux:chat)
同主路由间跳来跳去想把辅助路由干掉,用第二行便可

路由守卫

只有用户已经登录或者拥有某些权限才可进入的路由
canActive
1.写一个守卫类,继承 CanActivate 接口

import {CanActivate} from '@angular/router';
export class LoginGuard implements CanActivate {
canActivate() {
  let loginedIn: boolean = Math.random() < 0.5;
    if (!loginedIn) {
      console.log('用户未登录');
    }
    return loginedIn;
  }
}

 

这是CanDeactivate 与canActivate不一样的是它要离开某个组件就须要保护那个组件,创建也要注入那个组件

export class UnsaveGuard implements CanDeactivate<ProductComponent>{
canDeactivate (component: ProductComponent) {
  return window.confirm('是否离开');
}

 

这两个返回都应该是boolean
2.在路由配置里加配置
canActivate 在路由配置时能够配置一个数组,angular会一次调用数组中的项,一旦某个返回false,则会终止登录操做

{
path: 'product',
component: ProductComponent,
canActivate: [loginGuard],
canDeactivate: [UnsaveGuard]
}

 

服务里加上该服务

@NgModule({
imports: [RouterModule.forRoot(routes)],
providers: [LoginGuard, UnsaveGuard],
exports: [RouterModule]
})

 

路由守卫 很是重要 。再给个我网上找的参考文章
http://blog.csdn.net/qq451354/article/details/54017466

相关文章
相关标签/搜索