Angular 4 location.path 在组件之间导航

Angular 4 location.path to navigate between components

我最近用 angular 4 打字稿开始了一个项目。一切顺利,直到一点:

我需要创建路由原则。为此,我在 My App.Module.ts:

中创建了路由
RouterModule.forRoot([
      {path: '', component: HomeComponent },
      {path: 'thanks', component: ThanksComponent} //, canActivate: [AuthGuard]

    ])

在 angular js 中,为了从一个路由传递到另一个路由,我们使用了 routeConfig 然后在控制器中使用:

$location.path("/roue");

为了导航。

我的主要想法是在组件之间导航,就像我在 angular js 中使用导航一样。 如何在 Angular 4 中的组件之间导航?

您必须导入 Router 并使用

import { Router } from '@angular/router';

constructor(private _router: Router)
this.router.navigate("/thanks");

您可以使用

[routerLink]="['/roue']" 在你的 html

或在您的组件中:

import { Router } from '@angular/router';

constructor(
    private router: Router,
) { }

goToRoue(){
    this.router.navigate(['/roue']);
}

希望对您有所帮助!

@Sajeetharan 几乎 明白了。

使用

导入路由器
import { Router } from '@angular/router';

在你的组件中注入它

constructor(private router: Router) {}

现在你有多种调用方式了

this.router.navigate(['thanks']); // Array of routes
this.router.navigateByUrl('/thanks'); // Absolute path
this.router.navigate(['thanks', 1]); // route corresponding to thanks/1, useful for params