ng2路由跳转

本文主要介绍的是angular2中router路由跳转navigate的使用与刷新页面问题的相关内容,分享出供大家参考学习,下面来看看详细的介绍:

一、router.navigate的使用

navigate是Router类的一个方法,主要用来跳转路由。 

函数定义:

?
1
navigate(commands: any[], extras?: NavigationExtras) : Promise`<boolean>`
?
1
2
3
4
5
6
7
8
9
interface NavigationExtras {
  relativeTo : ActivatedRoute
  queryParams : Params
  fragment : string
  preserveQueryParams : boolean
  preserveFragment : boolean
  skipLocationChange : boolean
  replaceUrl : boolean
}

1.this.router.navigate(['user', 1]); 

以根路由为起点跳转

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

默认值为根路由,设置后相对当前路由跳转,route是ActivatedRoute的实例,使用需要导入ActivatedRoute

3.this.router.navigate(['user', 1],{ queryParams: { id: 1 } }); 
路由中传参数 /user/1?id=1

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

默认值为false,设为true,保留之前路由中的查询参数/user?id=1 to /view?id=1

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

路由中锚点跳转 /user/1#top

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

默认值为false,设为true,保留之前路由中的锚点/user/1#top to /view#top

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

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

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

未设置时默认为true,设置为false路由不会进行跳转

二、router.navigate刷新页面问题

造成这个问题一般是因为我们在<form>表单中使用<button>时忘记添加type属性,在表单中,如果忘记给按钮添加属性,会默认为submit

?
1
< button (click)="toDetail()">detail</ button >
?
1
2
3
toDetail() {
  this ._router.navigate([ '/detail' ]);
}

解决方法: 

1.添加type

?
1
< button type = "button" (click)="toDetail()">detail</ button >

2.click添加false

?
1
< button (click)="toDetail();false">detail</ button >

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用Angular.js能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。


在TypeScript (TS) 和 Angular 中,路由跳转通常通过`RouterModule`模块提供的服务和指令来完成。以下是一个简单的步骤概述: 1. **安装依赖**:首先,确保你在Angular项目中已经安装了`@angular/router`模块,如果还没有,可以运行`ng add @angular/router`。 2. **配置路由模块**:在`app-routing.module.ts`文件中,你需要导入`RouterModule`, `Routes`以及需要的组件。然后定义你的路由规则,如这样: ```typescript import { NgModule } from '@angular/core'; import { RouterModule, Routes } from '@angular/router'; import { HomeComponent } from './home/home.component'; import { AboutComponent } from './about/about.component'; const routes: Routes = [ { path: '', component: HomeComponent }, // 主页 { path: 'about', component: AboutComponent } // 关于页面 ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule {} ``` 3. **导航到特定路线**:在组件的HTML模板中,你可以使用`routerLink`指令来进行路由跳转,或者在组件内部使用`this.router.navigate`方法: ```html <!-- 使用routerLink --> <a routerLink="/about">关于</a> <!-- 或者在ts中动态跳转 --> <button (click)="goToAbout()">点击去关于</button> ngOnInit() { goToAbout(); } goToAbout() { this.router.navigate(['/about']); } ``` 4. **路由守卫(Optional)**:如果你想控制哪些用户、何时能访问某些路由,可以使用`CanActivate`守卫或其他类型的守卫。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值