美文网首页
ng4.x 子路由&辅助路由&路由守卫&resolve守卫

ng4.x 子路由&辅助路由&路由守卫&resolve守卫

作者: __凌 | 来源:发表于2017-10-21 10:40 被阅读0次

# 1 :重定向路由


 {path: '',redirectTo:'home', parthMatch:'full'},

 {path: 'home',component: HomeComponent}


# 2 :子路由


+ 形成插座的父子关系

+ 路由信息都是在模块层的,组件本身并不知道任何路由相关的消息

《app-routing.module.ts》

 {path: 'home',component: HomeComponent ,children:                                                                     [path:'',component:XxxComponent]}

<a [routerLink] = "['./']">商品描述</a>                                                                                        <a

<a [routerLink] = "['./seller', 99]">销售员信息 </a>                                                                      

<router-outlet></router-outlet>


#  3 :辅助路由


E1:在组件的模版上面添加一个带有那么属性的插座

E2:在路由配置里面配置name上可以显示哪些组件

{path:'xxx',component:XxxComponent,outlet:“aux”}

{path:'yyy',component:YyyComponent,outlet:“aux”}

E3:在导航的时候需要指定路由到某个地址的时候,辅助路由上需要显示哪个组件

<a [routerLink] = "['/home' , {outlets:{aux: 'xxx'}}]">Xxx</a>                                                     

<a [routerLink] = "['/product' , {outlets:{aux: 'yyy'}}]">Yyy </a>          

主插座显示home路由对应的组件,辅插座显示xxx路径对应的组件

eg

《app.component.html》

<a [routerLink] = "['/home']">主页</a>                                                                                          

<a [routerLink] = "['/product',1]">商品详情</a>                                                                          

<a [routerLink] = "[{outlets:{aux :'chart'}}]">开始聊天</a>                                          

<a [routerLink] = "[{outlets:{aux :null}}]">结束聊天</a>    

  <router-outlet></router-outlet>    

 <router-outlet name="aux"></router-outlet>

《app-routing.module.ts》:

{path:'chart',component:ChartComponent,outlet:“aux”}

扩:点击开始聊天时主路由跳到home组件

<a [routerLink] = "[{outlets:{primary: 'home',aux: 'chart'}}]">开始聊天</a>


# 4 :路由守卫


只有当用户满足了某些条件时才允许被进入或离开某个路由

如: 只有当用户已经登录并拥有某些权限时才能进入某些路由

E1:CanActivate:处理导航到某路由的情况

E2:CanDeactivate:处理从当前路由离开的情况

E3:Resolve:在路由激活之前获取路由数据

eg 1

login.guard.ts》:

import {CanActivate} from "@angular/router";

export class LoginGuard implements CanActivate {canActivate) {    

            let loggedIn:boolean = Math.random() < 0.5;

            if(!loggedIn) {console.log("用户未登录");}return loggedIn;

}

《app-routing.module.ts》:

{path:'product/:id',component:ProductComponent,children:                                                                 [{path:'seller/:id',component:SellerComponent}],  

            canActivate:[LoginGuard]

//angular 使用依赖注入机制来实例化LoginGuard

@NgModule({  

  ...    

...    

providers: [LoginGuard]

})

eg 2

《unSaved.guard.ts》:

import {CanDeactivate} from "@angular/router";

import {ProductComponent} from "../product/product.component";

export class UnsaveGuard implements CanDeactivate{

             canDeactivate(component: ProductComponent) {  

                             return window.confirm("你还未保存,确定要离开吗?");

}


# 5 :resolve守卫


使用resolve守卫可在进入路由之前先读取服务器数据,然后带着这些数据进入路由,立即在页面显示,避免http请求延时来来的较差的用户体验

eg:

《product.component.ts》

export class Product {constructor (public id: number, public name: string){}}

《product.resolve.ts》:

import {Resolve} from "@angular/router";import {Product} from "../product/product.component";

export  class ProductResolve  implements Resolve{                                                                   @Injectable()constructor(private router : Routert){}resolve (route: ActivatedRouteSnapshot, state:RouterStateSnapshot): observable|Promise|Product{

let productId:number = route.params["id"];

if(productId == 1){

return new Product(1,"iPhone7")

}else{

this.router.navigate(['/home']);

return undefined;

}}}

《app-routing.module.ts》:

{path:'product/:id',component:ProductComponent,children:[

{path:'seller/:id',component:SellerComponent}

],resolve:{

           product: ProductResolve

}

相关文章

  • ng4.x 子路由&辅助路由&路由守卫&resolve守卫

    # 1 :重定向路由 {path: '',redirectTo:'home', parthMatch:'full'...

  • vue的路由守卫

    路由守卫分3种:全局守卫路由独享守卫组件内的路由守卫 1.全局守卫:beforeEachbeforeResolve...

  • Vue嵌套路由和路由守卫

    嵌套路由 路由守卫

  • [VUE]动态的更新页面的Title

    首先可以用路由守卫 + VueRouter来实现路由守卫相关文档1.main.js里加入路由守卫 // 页面修改时...

  • 路由守卫

    路由守卫分为三种 全局守卫: 路由独享守卫卸载route里 组件内守卫:写在组件配置对象里

  • vue的路由和路由守卫

    router 和 路由守卫 路由配置 分模块配置 404 配置 懒加载 active-class 路由守卫(跟 a...

  • vue路由守卫

    概念 路由守卫,官网也叫导航守卫,所谓导航,就是路由正在发生变化 路由守卫,主要就是用来通过跳转或取消的方式守卫导...

  • angularjs路由守卫

    什么是路由守卫及路由守卫的用处: 路由守卫就是angularjs为我们提供的路于钩子函数,当我们进入或离开某个...

  • 路由

    全局守卫就是每一个路径进来都会经过这个全局守卫的处理 单路由守卫就是把守卫指定给某一个路由 组件内路由就是把守卫放...

  • vue-路由导航守卫&异步组件

    导航守卫包括全局导航守卫、路由守卫、组件局部守卫 全局导航守卫 在全局导航守卫中,每次路由的跳转他们三个都会被触发...

网友评论

      本文标题:ng4.x 子路由&辅助路由&路由守卫&resolve守卫

      本文链接:https://www.haomeiwen.com/subject/ukdbuxtx.html