Angular4中路由Router类的实例详解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Angular4中路由Router类的实例详解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2418字,纯文字阅读大概需要4分钟。
内容图文
![Angular4中路由Router类的实例详解](/upload/InfoBanner/zyjiaocheng/302/4872fa43261c42a3b84172f1ac26c7c5.jpg)
官方地址:
在学习的过程中路由(router)机制是离不开的,并且好多地方都要用到。
首先路由配置Route:
![Angular4中路由Router类的实例详解 - 文章图片](/upload/getfiles/0001/2021/4/22/20210422071630256.jpg)
![Angular4中路由Router类的实例详解 - 文章图片](/upload/getfiles/0001/2021/4/22/20210422071630299.jpg)
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 {}
其次路由跳转Router.navigate
1 navigate(commands: any[], extras?: NavigationExtras) : Promise<boolean>
![Angular4中路由Router类的实例详解 - 文章图片](/upload/getfiles/0001/2021/4/22/20210422071630330.jpg)
![Angular4中路由Router类的实例详解 - 文章图片](/upload/getfiles/0001/2021/4/22/20210422071630361.jpg)
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 : boolean10 }
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 });
还有好多好多东西需要学习,关于跳转就先写到这里了,希望大家共同学习分享踏过的坑。
以上就是Angular4中路由Router类的实例详解的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的Angular4中路由Router类的实例详解全部内容,希望文章能够帮你解决Angular4中路由Router类的实例详解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。