Angular 2 - 如何使用导航到另一条路线this.router.parent.navigate('/about')?
this.router.parent.navigate('/about')
它似乎不起作用。我试过location.go("/about");了,因为没有用。
location.go("/about");
基本上,一旦用户登录,我想将他们重定向到另一个页面。
下面是我的代码:
import {Component} from 'angular2/angular2'; import {CORE_DIRECTIVES, FORM_DIRECTIVES} from 'angular2/angular2'; import {Router} from 'angular2/router'; import {AuthService} from '../../authService'; //Model class User { constructor(public email: string, public password: string) {} } @Component({ templateUrl:'src/app/components/todo/todo.html', directives: [CORE_DIRECTIVES, FORM_DIRECTIVES] }) export class Todo { model = new User('Mark@gmail.com', 'Password'); authService:AuthService; router: Router; constructor(_router: Router, _authService: AuthService){ this.authService = _authService; this.router = _router; } onLogin = () => { this.authService.logUserIn(this.model).then((success) => { //This is where its broke - below: this.router.parent.navigate('/about'); }); } }
绝对路径路由
有两种导航方法,.navigate()和.navigateByUrl()
.navigate()
.navigateByUrl()
您可以使用.navigateByUrl()绝对路径路由的方法:
import {Router} from '@angular/router'; constructor(private router: Router) {} navigateToLogin() { this.router.navigateByUrl('/login'); }
您将绝对路径放入要导航到的组件的 URL。
注意:调用路由器的navigateByUrl方法时一定要指定完整的绝对路径。绝对路径必须以前导开头/
navigateByUrl
/
// Absolute route - Goes up to root level this.router.navigate(['/root/child/child']); // Absolute route - Goes up to root level with route params this.router.navigate(['/root/child', crisis.id]);
相对路径路由
如果要使用相对路径路由,请使用.navigate()方法。
注意:路由的工作方式有点不直观,尤其是父路由、兄弟路由和子路由:
// Parent route - Goes up one level // (notice the how it seems like you're going up 2 levels) this.router.navigate(['../../parent'], { relativeTo: this.route }); // Sibling route - Stays at the current level and moves laterally, // (looks like up to parent then down to sibling) this.router.navigate(['../sibling'], { relativeTo: this.route }); // Child route - Moves down one level this.router.navigate(['./child'], { relativeTo: this.route }); // Moves laterally, and also add route parameters // if you are at the root and crisis.id = 15, will result in '/sibling/15' this.router.navigate(['../sibling', crisis.id], { relativeTo: this.route }); // Moves laterally, and also add multiple route parameters // will result in '/sibling;id=15;foo=foo'. // Note: this does not produce query string URL notation with ? and & ... instead it // produces a matrix URL notation, an alternative way to pass parameters in a URL. this.router.navigate(['../sibling', { id: crisis.id, foo: 'foo' }], { relativeTo: this.route });
或者,如果您只需要在当前路由路径中导航,但需要导航到不同的路由参数:
// If crisis.id has a value of '15' // This will take you from `/hero` to `/hero/15` this.router.navigate([crisis.id], { relativeTo: this.route });
链接参数数组
链接参数数组包含以下用于路由器导航的成分:
['/hero']
['/hero', hero.id]
['/hero', { id: hero.id, foo: baa }]
类目录语法
路由器在链接参数列表中支持类似目录的语法,以帮助指导路由名称查找:
./或没有前导斜线相对于当前级别。
./
../在路线路径上上一层。
../
您可以将相对导航语法与祖先路径结合起来。如果您必须导航到同级路由,您可以使用../<sibling>约定上一级,然后上下同级路由路径。
../<sibling>
关于相对导航的重要说明
要使用该方法导航相对路径Router.navigate,您必须提供ActivatedRoute以使路由器知道您在当前路由树中的位置。
Router.navigate
ActivatedRoute
在链接参数数组之后,添加一个relativeTo属性设置为ActivatedRoute. 然后路由器根据活动路由的位置计算目标 URL。
relativeTo
来自官方Angular 路由器文档