惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
罗磊的独立博客
F
Fortinet All Blogs
人人都是产品经理
人人都是产品经理
量子位
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
B
Blog RSS Feed
腾讯CDC
博客园_首页
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
博客园 - Franky
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
D
Docker
P
Privacy & Cybersecurity Law Blog
S
Securelist
V
V2EX
Jina AI
Jina AI
阮一峰的网络日志
阮一峰的网络日志
T
Tor Project blog
The Hacker News
The Hacker News
Microsoft Azure Blog
Microsoft Azure Blog
AWS News Blog
AWS News Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Help Net Security
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Recent Announcements
Recent Announcements
Cloudbric
Cloudbric
Y
Y Combinator Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
V2EX - 技术
V2EX - 技术

博客园 - ZEKELOVE

学习云计算基础总结 初识物联网的无线(长/短)距离技术总结 初识物联网知识入门总结 学习React框架-总结 WEB开发-HTML入门学习总结 WEB开发-CSS入门学习总结 WEB开发-HTML入门学习总结 华为RPA机器人学习(2) 华为RPA机器人学习(1) Python学习(1)-基础语法学习丨【生长吧!Python】 《Python入门》学习笔记(3) 《Python入门》学习笔记(2) 《Python入门》学习笔记(1) 《基于华为云DevCloud的托马斯商城》的学习笔记 Vue+Vue-router微信分享功能 2017浅谈面试(一) iscroll简单使用说明 jQuery File Upload跨域上传 文本框输入内容放大显示和格式化插件
Angular4中路由Router类的跳转navigate
ZEKELOVE · 2017-06-09 · via 博客园 - ZEKELOVE

最近一直在学习angular4,它确实比以前有了很大的变化和改进,好多地方也不是那么容易就能理解,好在官方的文档和例子是中文,对英文不太好的还是有很大帮助去学习。

官方地址:https://angular.cn/docs/ts/latest/api/router/index/Router-class.html

在学习的过程中路由(router)机制是离不开的,并且好多地方都要用到。

首先路由配置Route:

 1 import { NgModule }             from '@angular/core';
 2 import { RouterModule, Routes } from '@angular/router';
 3  
 4 import { HomeComponent }   from './home.component';
 5 import { LoginComponent }      from './login.component';
 6 import { RegisterComponent }  from './register.component';
 7  
 8  const routes: Routes = [
 9    { path: '', redirectTo: '/home', pathMatch: 'full' },
10    { path: 'home',  component: HomeComponent },
11    { path: 'login', component: LoginComponent },
12    { path: 'heroes',     component: RegisterComponent }
13  ];
14  
15  @NgModule({
16    imports: [ RouterModule.forRoot(routes) ],
17    exports: [ RouterModule ]
18  })
19  export class AppRoutingModule {}

View Code

其次路由跳转Router.navigate

1 navigate(commands: any[], extras?: NavigationExtras) : Promise<boolean>

 1 interface NavigationExtras {
 2     relativeTo : ActivatedRoute
 3     queryParams : Params
 4     fragment : string
 5     preserveQueryParams : boolean
 6     queryParamsHandling : QueryParamsHandling
 7     preserveFragment : boolean
 8     skipLocationChange : boolean
 9     replaceUrl : boolean
10 }

View Code

1.以根路由跳转/login

this.router.navigate(['login']);

2.设置relativeTo相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

this.router.navigate(['login', 1],{relativeTo: route}); 

3.路由中传参数 /login?name=1

this.router.navigate(['login', 1],{ queryParams: { name: 1 } }); 

4.preserveQueryParams默认值为false,设为true,保留之前路由中的查询参数/login?name=1 to /home?name=1

this.router.navigate(['home'], { preserveQueryParams: true }); 

5.路由中锚点跳转 /home#top

 this.router.navigate(['home'],{ fragment: 'top' });

6.preserveFragment默认为false,设为true,保留之前路由中的锚点/home#top to /role#top

this.router.navigate(['/role'], { preserveFragment: true }); 

7.skipLocationChange默认为false,设为true,路由跳转时浏览器中的url会保持不变,但是传入的参数依然有效

this.router.navigate(['/home'], { skipLocationChange: true });

8.replaceUrl默认为true,设为false,路由不会进行跳转

this.router.navigate(['/home'], { replaceUrl: true }); 

还有好多好多东西需要学习,关于跳转就先写到这里了,希望大家共同学习分享踏过的坑。

作者:zeke     
          出处:http://zhf.cnblogs.com/
          本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。