We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
import { Router, ActivatedRoute } from '@angular/router'; export class xxx{ constructor(private router: Router, private route: ActivatedRoute){} }
navigateByUrl(url: string | UrlTree, extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>
第一个参数必须是==绝对路径==的字符串。
this.router.navigateByUrl('/home');
他俩接收的第一个参数不同,第二个参数相同。
navigate(commands: any[], extras: NavigationExtras = { skipLocationChange: false }): Promise<boolean>
第一个参数是一个数组
this.router.navigate(['home', 'demo'])
那么解析的路由就是localhost:4200/home/demo。
localhost:4200/home/demo
可以相对当前路由进行导航
传入一个relativeTo参数,即可相对传入的路由进行跳转。如当前在localhost:4200/home,
relativeTo
localhost:4200/home
this.router.navigate(['demo'], {relativeTo: this.route})
跳转后的地址为localhost:4200/home/demo。
但如果'demo'写成'/demo'传入的路由就不起作用了,会以根路由进行导航。如果不传入也是默认以根路由(localhost:4200)进行导航。
'demo'
'/demo'
interface NavigationExtras { relativeTo?: ActivatedRoute | null queryParams?: Params | null fragment?: string preserveQueryParams?: boolean queryParamsHandling?: QueryParamsHandling | null preserveFragment?: boolean skipLocationChange?: boolean replaceUrl?: boolean state?: {...} }
以 navigate 举例
此种传参方式会把参数拼接在url上,如localhost:4200/demo?id=1
localhost:4200/demo?id=1
A组件传递参数
this.router.navigate(['demo'], {queryParams: {id: 1} , relativeTo: this.route})
B组件接收参数
/user/:id
activatedRoute.params
import { ActivatedRoute } from '@angular/router'; constructor(private activatedRoute: ActivatedRoute) { this.activatedRoute.params.subscribe((param) => { console.log('组件里面的param', param);// {id :1} }); }
/user?id=1
import { ActivatedRoute } from '@angular/router'; constructor(private activatedRoute: ActivatedRoute) { this.activatedRoute.queryParams.subscribe((param) => { console.log('组件里面的queryParams', param); // {id :1} }); }
此种方式会把数据存在浏览器的历史记录中,state必须是一个对象,在子路由中使用getCurrentNavigation取出。
import { Component, Input } from '@angular/core'; import { Router, NavigationExtras } from '@angular/router'; @Component({ selector: 'a-component', template: ` <button (click)="test()">Test</button> `, }) export class AComponent { constructor(private router: Router){} test(){ const navigationExtras: NavigationExtras = {state: {id: 1}}; this.router.navigate(['b'], navigationExtras); } }
import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector: 'b-component' }) export class BComponent { constructor(private router: Router) { const navigation = this.router.getCurrentNavigation(); const state = navigation.extras.state as {id: number}; // state.id 就是传过来的数据 } }
2.2 均有回调
this.router.navigate(['demo']).then(nav => { console.log(nav); // true: 跳转成功, false 跳转失败 }, err => { console.log(err) // 发生无措 });
The text was updated successfully, but these errors were encountered:
No branches or pull requests
angular navigateByUrl vs navigate 路由跳转
1. 不同点
1.1 navigateByUrl()
第一个参数必须是==绝对路径==的字符串。
他俩接收的第一个参数不同,第二个参数相同。
1.2 navigate()
第一个参数是一个数组
那么解析的路由就是
localhost:4200/home/demo
。可以相对当前路由进行导航
传入一个
relativeTo
参数,即可相对传入的路由进行跳转。如当前在localhost:4200/home
,跳转后的地址为
localhost:4200/home/demo
。但如果
'demo'
写成'/demo'
传入的路由就不起作用了,会以根路由进行导航。如果不传入也是默认以根路由(localhost:4200)进行导航。2. 共同点:
2.1 传递参数方式一样
通过queryParams传参
此种传参方式会把参数拼接在url上,如
localhost:4200/demo?id=1
A组件传递参数
B组件接收参数
/user/:id
方式传递过来用activatedRoute.params
/user?id=1
方式传递用activatedRoute.params
通过state传参
此种方式会把数据存在浏览器的历史记录中,state必须是一个对象,在子路由中使用getCurrentNavigation取出。
A组件传递参数
B组件接收参数
2.2 均有回调
The text was updated successfully, but these errors were encountered: