angualr4 路由 总结笔记
使用cli命令创建根路由模块 ng g cl app.router 或自己建一个路由配置文件 如:app/app.router.ts
// app/app.router.ts
// 将文件修改为 import { RouterModule, Routes } from '@angular/router'; const routes: Routes = [
// 以后在这里改动配置
{ path: '**', pathMatch: 'full', redirectTo: '' }
];
// 将路由配置导出为 appRouting 以供调用, 子模块中的路由使用 forChild 而不是 forRoot
export const appRouting = RouterModule.forRoot(routes);
在根模块中引入路由, 为特性模块定义的路由在其模块中引入
// app/app.module.ts
import { appRouting } from "./router/router.module"
// @NgModule({
// imports: [ ... ,
appRouting
// ...
路由配置
const routes: Routes = [
// path:路径 /news component:组件
{ path: 'news', component: Newsomponent },
// path:路径 /detail/1 component:组件
{ path: 'detail/:id', component: DetailComponent },
// 懒加载子模块, 子模块需要配置路由设置启动子组件
{ path: 'other', loadChildren:"./other/other.module#otherModule" },
// 重定向
{ path: '**', pathMatch: 'full', redirectTo: '' }
];
pathMatch?: string; 默认为前缀匹配 "prefix"; "full" 为完全匹配
outlet?: string; 路由目标
children?: Routes; 子路由的规则
加载路由
在根组件或当前组件的模板中
<router-outlet></router-outlet>
多个路由区域
{ path: 'news', outlet:'let1' component: NewsComponent }
{ path: 'news', outlet:'let2' component: News2Cmponent }
<router-outlet name="let1"></router-outlet>
<router-outlet name="let2"></router-outlet>
即访问 /news/ 时同时加载 NewsComponent 和 News2Cmponent 两个组件
链接及访问
<a routerLink="/detail/1" routerLinkActive="active">detail</a>
<a [routerLink]="['/detail', news.id]">{{news.title}}</a>
<a [routerLink]="[{ outlets: { let2: ['news'] } }]">Contact</a>
routerLinkActive="active" 即在本路由激活时添加样式 .active
或
import { Router } from '@angular/router';
// ...
constructor(private router: Router) {}
// ...
this.router.navigate(['/detail', this.news.id])
this.router.navigate([{ outlets: { let2: null }}]);
navigateByUrl 方法指向完整的绝对路径
路由参数
const appRoutes: Routes = [
....
{path: 'product/:id',canActivate: [AuthService], component: ProductComponent},
....
];
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute} from '@angular/router';
import 'rxjs/add/operator/switchMap';
@Component({
selector: 'app-product',
templateUrl: './product.component.html',
styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {
public id: number;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.route.params.subscribe((params) => this.id = params.id);
console.log(this.id)
}
}
路由守卫
适用于后台管理等需要登录才能使用的模块
创建一个认证服务
// app/auth.service.ts
import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router';
@Injectable()
export class AuthService implements CanActivate {
canActivate() {
// 这里判断登录状态, 返回 true 或 false
return true;
}
}
添加或修改路由配置
// app/app.router.ts // 增加 CanActivate
import { CanActivate ... } from '@angular/router'; // 配置中增加 canActivate 如:
{ path: 'admin', canActivate:[AuthService] ... }
退出守卫
适合于编辑器修改后的保存提示等场景
// app/deac.service.ts
import { Injectable } from '@angular/core';
import { CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
// CanDeactivateComponent 是定义的接口,见下段代码
import { CanDeactivateComponent } from './can-deactivate.omponent';
@Injectable()
export class DeacService implements CanDeactivate<CanDeactivateComponent> {
canDeactivate(
canDeactivateComponent: CanDeactivateComponent,
activatedRouteSnapshot: ActivatedRouteSnapshot,
routerStateSnapshot: RouterStateSnapshot
) {
// 目标路由和当前路由
console.log(activatedRouteSnapshot);
console.log(routerStateSnapshot);
// 判断并返回
return canDeactivateComponent.canDeactivate ? canDeactivateComponent.canDeactivate() : true
}
}
// can-deactivate.omponent.ts // 接口组件, 返回 true 或 false 如表单发生改变则调用对话框服务
export interface CanDeactivateComponent {
canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
路由配置
{
path: ...,
canDeactivate: [DeacService],
component: ...
}
模块中添加服务
providers: [
DeactivateGuardService
]
angualr4 路由 总结笔记的更多相关文章
- Angular2之路由学习笔记
目前工作中项目的主要技术栈是Angular2 在这里简单记录一下遇到的问题以及解决方案. 这篇笔记主要记录Angular2 的路由. 官方文档链接:https://angular.cn/docs/ts ...
- vue2.0 路由学习笔记
昨天温故了一下vue2.0的路由 做个笔记简单记录一下! 1.首相和vue1.0一样 要使用vuejs的路由功能需要先引入vue-router.js 2.然后修改原有a标签处代码 这里以一个ul li ...
- CCIE-交换路由复习笔记
交换 考点: 1.trunk link(基础) 2.vtp 3.vlan 4.stp rstp mstp 5.hsrp vrrp glbp 6.ec Trunk link: 修改封装模式 802.1q ...
- 一个C#程序员学习微信小程序路由的笔记
路由大家应该都知道,在微信小程序也是有的,毕竟它是单页面应用程序.在WeChat中有五种跳转方式,分别是wx.switchTab.wx.reLaunch.wx.redirectTo.wx.naviga ...
- Vue路由学习笔记
Vue路由大致分为6个步骤: 1.引用vue-router <script src="js/vue-router.js"></script> 2.安装插件 ...
- Linux 路由 学习笔记 之一 相关的数据结构
http://blog.csdn.net/lickylin/article/details/38326719 从现在开始学习路由相关的代码,在分析代码之前, 我们还是先分析数据结构,把数据结构之间的关 ...
- angular4路由设置笔记
场景说明:angular4开发的一个后台项目 一.可以使用angular-cli创建一个带路由的项目,ng new 项目名称 --routing 会多创建一个app-routing.module.ts ...
- angular路由学习笔记
文章目录 标签routerLink路由传递参数 url中get传值 定义路由 获取参数 配置动态路由 定义路由 获取参数 API js路由跳转 配置动态路由 定义路由 获取参数 get传值 定义路由 ...
- vue.js路由学习笔记二
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
随机推荐
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
题目链接:http://codeforces.com/problemset/problem/847/I I. Noise Level time limit per test 5 seconds mem ...
- python中strip函数的用法
python中往往使用剥除函数strip()来对用户的输入进行清理.strip函数的最一般形式为: str.strip('序列') 其中,序列是一段字符串,该函数表示从头或者从尾部开始进行扫描,如果扫 ...
- Python实战之列表list的详细简单练习2
name_list = ['zhangsan','lisi','wangermazi','xiaotaoqi'] print(name_list) # name_list.append("w ...
- Python tkinter调整元件在窗口中的位置与几何布局管理
Tkinter中的GUI总是有一个root窗口,不管你是主动或者别动获得.主窗口就是你的程序开始运行的时候创建的,在主窗口中你通常是放置了你主要的部件.另外,Tkinter脚本可以依据需要创建很多独立 ...
- zoj1494 暴力模拟 简单数学问题
Climbing Worm Time Limit: 2 Seconds Memory Limit:65536 KB An inch worm is at the bottom of a we ...
- 数据库中删除语句Drop、Delete、Truncate的相同点和不同点的比较
数据库删除语句的分别介绍: Delete:用于删除表中的行(注:可以删除某一行:也可以在不删除表的情况下(即意味着表的结构.属性.索引完整)删除所有行) 语法:删除某一行:Delete From 表名 ...
- 提纲挈领webrtc之vad检测
顾名思义,VAD(Voice Activity Detection)算法的作用是检测是否是人的语音,它的使用 范围极广,降噪,语音识别等领域都需要有vad检测.vad检测有很多方法,这里我们之介绍一 ...
- Zeppelin源码
1.概述 在大数据应用场景中,处理数据分析方面,由于开发者的水平不一样,使用的编程语言也不尽相同,可能会涉及到R.Python.Java.Scala等,数据计算模型也估计不一样,可能涉及的有Spark ...
- js的解析顺序 作用域 严格模式
一.javascript的解析顺序 我们大家所理解的代码的执行顺序都是从上到下的,但是实际上确不是这样的.我们看一下下面的代码. 1 alert(a); 2 var a = 1; 如果执行顺序是从上到 ...
- 《Unity3D/2D游戏开发从0到1(第二版本)》 书稿完结总结
前几天,个人著作<Unity3D/2D游戏开发从0到1(第二版)>经过七八个月的技术准备以及近3个月的日夜编写,在十一长假后终于完稿.今天抽出一点时间来,给广大热心小伙伴们汇报一下书籍概况 ...