美文网首页
路由守卫

路由守卫

作者: 2764906e4d3d | 来源:发表于2019-01-19 09:21 被阅读0次

子路由

  1. 新建两个组件,商品描述组件和销售员信息,进入商品详情页面会默认展示商品描述
{path:'product',component:ProductComponent,children:[
    {path:'',component:ProductDescComponent},
    {path:'seller/:id',component:sellerInfo},
  ]},

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

<router-outlet></router-outlet>
  1. 获取商品的id
export  class SellerInfoComponent implements  OnInit{
  private sellerId:number;
  constructor(private  routeInfo:ActivatedRoute){}

  ngOnInit(){
    this.sellerId=this.routeInfo.snapshot.params["id"];
  }
}

辅助路由

  1. 这个路由的存在和消失并不是由主路由来控制的,而是单独控制的,主路由之间相互切换,它都会一直存在/消失
  2. 在任何页面中都有一个聊天控件随时可以聊天,在App组件的模板上在定义一个插座(<router-outlet>)来显示聊天面板,单独开发一个聊天组件,只显示新定义的插座上,通过路由参数 控制新插座是否显示聊天面板
    (设置聊天组件的样式大小为30%,其他所有组件使用div包裹占据70%)
  3. 在app模板中使用router-outlet的name指向一个aux,在定义路由的路径是会多出一个属性outlet,使它的值和router-outlet的name的值相同
<router-outlet></router-outlet>
<router-outlet name="aux"></router-outlet>

{path:'chat',component:ChatComponent,outlet:'aux'},
  1. 设置这个聊天组件是否显示
<a [routerLink]="[{outlets:{aux:'chat'}}]">开始聊天</a>
<a [routerLink]="[{outlets:{aux:null}}]">结束聊天</a>
  1. 点击开始聊天的时候,在显示聊天组件的同时也会跳转到home路由
<a [routerLink]="[{outlets:{primary:'home',aux:'chat'}}]">开始聊天</a>

路由守卫

  1. 当用户满足某些条件是才会被允许进入某些路由,例如只有当用户已经登录时并拥有某些权限才能进入某些路由,一个由多个表单组件组成的向导,例如注册的流程,用户只有在满足当前路由要求填写的信息才能进入下一步导航到下一个路由,当用户未执行保存操作而试图离开时提醒用户
  • CanActivate:处理导航到某路由的情况(当不能满足这个守卫的要求,就不能导航到指定的路由)
  • CanDeactivate:处理从当前路由离开的情况(当不能满足这个守卫的要求,就不能从当前路由离开)
  • Resole:在路由激活之前获取路由数据
  1. 在app文件夹下新建一个guard文件夹新建一个login.guary.ts文件(随机生成0-1的数模拟用户登录,如果大于0.5则是未登陆)
import {CanActivate} from "@angular/router"
export class LoginGuard implements  CanActivate{
  CanActivate() {
    let loggedIn:boolean=Math.random() < 0.5;
    if(!loggedIn){
      console.log("用户未登陆");
    }
    return undefined;
  }
}
  1. 在路由模板中判断如果有一个为false就会被拒绝访问,并且在模块中再声明一次LoginGuard
{path:'product',component:ProductComponent,children:[
    {path:'',component:ProductDescComponent},
    {path:'seller/:id',component:sellerInfo}
  ],CanActivate:[LoginGuard],
   },

@NgModule({
    providers:[LoginGuard]
})
  1. 新建一个unsave.guary.ts文件,当用户试图离开产品信息组件时
import {CanDeactivate} from "@angular/router"
import {ProductComponent} from "../product/product.component"
export class UnsaveGuard implements  CanDeactivate<ProductComponent>{
  CanDeactivate() {
    return window.confirm("未保存")
    }
  
}
{path:'product',component:ProductComponent,children:[
    {path:'',component:ProductDescComponent},
    {path:'seller/:id',component:sellerInfo}
  ],CanActivate:[LoginGuard],
    CanDeactivate:[UnsaveGuard]
},
providers:[LoginGuard,UnsaveGuard]
  1. 新建一个product.resolve.ts,获取商品的id,如果id不为1 则会 返回到home
import {Resolve,ActivatedRouteSnapshot,RouterStateSnapshot,Router} from "@angular/router"
import {ProductComponent} from "../product/product.component";
import {Observable} from "rxjs";
import {Injectable} from "@angular/core"

@Injectable()
export class ProductResolve implements Resolve<Product>{

  constructor(private router:Router){}

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): <Product> | Promise<Product> | Product {

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

    if(productId==1){
      return new Product(1,"niz")
    }else{
      this.router.navigate(['/home'])
    }

    return  undefined;
  }

}
  1. 在商品信息模块中有一个product对象
export class Product {
  constructor(public id:number,public name:string)
}
  1. 将守卫加入到路由中,并同样在providers中声明,在商品模块中声明并且获取商品name与之前声明的id一致
{path:'product',component:ProductComponent,children:[
    {path:'',component:ProductDescComponent},
    {path:'seller/:id',component:sellerInfo}
  ],resolve:{
    product:ProductResolve
  }
},

相关文章

  • vue的路由守卫

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

  • 路由守卫

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

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

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

  • vue学习笔记(八)导航守卫(全局守卫,路由独享,组件内守卫)

    导航守卫-全局守卫 导航守卫-路由独享 导航守卫-组件内守卫

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

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

  • Vue嵌套路由和路由守卫

    嵌套路由 路由守卫

  • vue路由守卫

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

  • vue学习(52)vue-router(4)

    路由守卫作用:对路由进行权限控制分类:全局守卫、独享守卫、组件内守卫全局守卫(我们可以在meta中配置一些信息) ...

  • 路由

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

  • vue-router 常见导航守卫

    全局守卫vue-router全局有三个守卫 路由独享守卫如果你不想全局配置守卫的话,你可以为某些路由单独配置守卫 ...

网友评论

      本文标题:路由守卫

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