【发布时间】:2016-02-07 21:13:13
【问题描述】:
Angular 2 - 如何使用 this.router.parent.navigate('/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');
});
}
}
【问题讨论】:
-
另外,我在 app.ts 文件中设置了路由配置,如下所示:@RouteConfig([ { path: '/', redirectTo: '/home' }, { path: '/ home', 组件: Todo, as: 'Home' }, { path: '/about', 组件: About, as: 'About' } ])
-
您应该删除路径中的
/,因为它不是必需的
标签: angular typescript angular2-routing