当前位置: 移动技术网 > IT编程>脚本编程>AngularJs > 详解angular笔记路由之angular-router

详解angular笔记路由之angular-router

2017年12月12日  | 移动技术网IT编程  | 我要评论

toshi小弟吧,yy影音盒,孙小宝黑吃黑高清

本文介绍了angular笔记路由之angular-router,分享给大家,具体如下:

创建项目

ng new router --routing 
\\ 加routing参数
\\ 会新增一个app-routing.module.ts 文件

路由的基本使用

名称 简介
routes 路由的配置,保存着哪一个url对应展示哪一个组件和在哪一个routeroutler展示
routeroutler 在html中标记路由内容呈现的占位符指令
router 运行时执行的路由对象,可以通过navigate()和navigatebyurl()方法来导航到指定路由
routerlink 在html中声明导航的指令
activatedroute 当前激活的路由对象,保存着当前路由信息,路由地址,路由参数

路由对象图示


路由基本配置

const routes:routes = [
 {path:'',component:homecomponent}, \\ 注意path里面不要加\线
 {path:'app',component:admincomponent}
];

路由通配符配置

{path:'**',component:code404component}
// 配置里面加一条代表没有的都往这里,注意这里不能写在前面,不然angualr的路由会优先匹配这里

html里面跳转链接

<a [routerlink]="['/']">主页</a>
<a [routerlink]="['/app']">后台</a>
<router-outlet></router-outlet>

在js里面跳转路由

<input type='button' value='跳转到后台' (click)="toapp()">
constructor(private router:router){
 }
// 点击事件
 toapp(){
 this.router.navigate(['/app'])
 }

路由数据传递

1、在查询参数中传递数据

/app?id=2 => activatedroute.queryparams[id]
//html写法
<a [routerlink]="['/app']" [queryparams]="{id:1}">后台</a>
// js写法
private appid:number
 constructor(private routerinfo:activatedroute) { }

 ngoninit() {
 this.appid = this.routerinfo.snapshot.queryparams['id']
 }

2、在路由路径中传递数据

{path:/app/:id} => /app/1 => activatedroute.params[id]
// 必须先定义好
<a [routerlink]="['/app',1]">后台</a>

3、在路由配置中传递数据

复制代码 代码如下:

{path:/product,component:appcomponent,data:[isprod:true]}  => activatedroute.data[0][isprod]

参数快照和参数订阅

snapshot 是参数快照当路由进入该组件的时候,然后再点击按钮进入该路由路由里面的的ngoninit()方法只执行一次,已经被激活,说以第二次这个不会被执行

ngoninit() {
 this.appid = this.routerinfo.snapshot.queryparams['id']
 }

subscribe 是参数订阅,这个属于rxjs的东西

private appid:number

 constructor(private routerinfo:activatedroute) { }

 ngoninit() {
 this.routerinfo.params.subscribe((params:params) => {
  this.appid = params['id']
 })
 }

重定向路由

{path:'',redirectto:'/home',pathmatch:'full'},

子路由

{path:'home',component:homecomponent,children:[
 {path:'',component:indexcomponent}
 ]},
// 记得去homecomponent,里面添加<router-outlet></router-outlet>指令

辅助路由

// html 视图部分
<router-outlet></router-outlet>
<router-outlet name='aux'></router-outlet>
//路由配置部分
{path:'app',appcomponet,outlet:'aux'}

路由守卫

只有当用户已经登录并拥有一些权限时才能进入

多表单组成的向导,如注册流程,只有满足条件才能进入下一个路由

用户执行操作没有保存,试图离开时候进行相关提示

名称 说明
canaxtivate 处理导航到某路由
candeactivate 处理当前路由离开
resolve 在路由激活前获取路由数据

1.canaxtivate的使用

// 新建一个文件
import {canactivate} from '@angular/router'
export class loginguard implements canactivate{
 // 路由会根据这个方法的返回如果返回false就拒绝访问
 canactivate(){
 let islogin:boolean = true;
 return islogin;
 }
}

canactivate是一个数组,可以接收多个,当每一个都返回true时候才允许

// 修改路由配置,添加一个属性canactivate
{path:'home',component:homecomponent,children:[
 {path:'',component:indexcomponent}
 ],canactivate:[loginguard]},
修改ngmodule

providers:[loginguard]

2.candeactivate的使用

// 新建一个文件
import {candeactivate} from '@angular/router'
import {appcomponent} from "../app.component";

/**
 * 处理用户离开
 * 接收一个泛型
 */
export class outguard implements candeactivate<appcomponent>{
 // component 里面保存着appcomponent的数据
 candeactivate(component:appcomponent){
 return window.confirm('您还没有保存确定要离开吗?')
 }
}

修改路由的配置

{path:'home',component:homecomponent,children:[
 {path:'',component:indexcomponent}
 ],canactivate:[loginguard],candeactivate:[outguard]},

providers:[loginguard,outguard]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网