这篇文章主要介绍了详解angular4中路由router类的跳转navigate,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近一直在学习angular4,它确实比以前有了很大的变化和改进,好多地方也不是那么容易就能理解,好在官方的文档和例子是中文,对英文不太好的还是有很大帮助去学习。
在学习的过程中路由(router)机制是离不开的,并且好多地方都要用到。
首先路由配置Route:
import { NgModule } from '@angular/core';import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home.component';import { LoginComponent } from './login.component';import { RegisterComponent } from './register.component'; const routes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', component: HomeComponent }, { path: 'login', component: LoginComponent }, { path: 'heroes', component: RegisterComponent } ]; @NgModule({ imports: [ RouterModule.forRoot(routes) ], exports: [ RouterModule ] }) export class AppRoutingModule {}
登录后复制
其次路由跳转Router.navigate
navigate(commands: any[], extras?: NavigationExtras) : Promise
登录后复制
interface NavigationExtras { relativeTo : ActivatedRoute queryParams : Params fragment : string preserveQueryParams : boolean queryParamsHandling : QueryParamsHandling preserveFragment : boolean skipLocationChange : boolean replaceUrl : boolean}
登录后复制
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类的跳转navigate的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2764291.html