gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:42:21 24 4
gpt4 key购买 nike

我最近开始了一个使用 Angular 4 和 TypeScript 的项目。一切都很顺利,直到一点:

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

RouterModule.forRoot([
{path: '', component: HomeComponent },
{path: 'thanks', component: ThanksComponent} //, canActivate: [AuthGuard]

])

在 Angular js 中,为了从一个路由传递到另一个路由,我们使用了 routeConfig ,然后在 Controller 中使用:

$location.path("/roue");

为了导航。

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

最佳答案

@Sajeetharan 几乎明白了。

使用

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

将其注入(inject)到您的组件中

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

关于Angular 4 location.path 在组件之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48164527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com