【问题标题】:How to trace routing in Angular 2?如何在 Angular 2 中跟踪路由?
【发布时间】:2018-01-21 23:14:21
【问题描述】:

我的组件带有单独的路由设置文件:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { Route } from '../core/route.service';
import { extract } from '../core/i18n.service';
import {CalendarThematicPlanComponent} from './calendar-thematic-plan.component';

const routes: Routes = Route.withShell([
  { path: 'calendar', component: CalendarThematicPlanComponent }
]);

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
  providers: []
})

export class CalendarThematicPlanRoutingModule { }

当我输入 URL 地址时:http://localhost:4200/calendar 我被重定向到主页。

如何在 Angular 2 中跟踪路由?

【问题讨论】:

  • 为什么我们在这里使用 Route.withShell?

标签: angular angular-routing


【解决方案1】:

你可以传入a second argumentoptions:

imports: [
    RouterModule.forRoot(
      routes,
      { enableTracing: true } // <-- debugging purposes only
    )
]

然后,Angular 将根据文档将所有事件记录到浏览器的控制台:

enableTracing?:布尔值
如果为 true,则将所有内部导航事件记录到控制台。用于调试。

【讨论】:

  • 我也使用了另一种结构:@NgModule({ imports: [RouterModule.forChild(routes)], exports: [RouterModule], providers: [] })
  • 在浏览器的控制台中
  • 我在控制台中什么也看不到
  • 这有助于我调试组件模板的问题。我正在单击一个链接,但没有发生任何事情,控制台上也没有任何迹象。只需添加此选项即可在控制台上生成一个非常详细且有用的 NavigationError 事件。
  • @PRMan 这不是 forChild() 中的一个选项(截至 2019 年 8 月)
【解决方案2】:

正如最被接受的答案中的 cmets 所暗示的,这个 enableTracingforChild 方法中不起作用。一个简单的解决方法是订阅AppModule 中的所有路由事件,如下所示:

export class AppModule {

  constructor(
    private readonly router: Router,
  ) {
    router.events
      .subscribe(console.log)
  }

}

【讨论】:

    【解决方案3】:

    除了 devqons 出色的答案:如果您暂时取消注释通配符路由,调试您的路由定义会容易得多。通配符路线在生产中很方便显示,例如NotFound 组件,但调试时很痛苦。

    例如:

    const routes: Routes = [
        ... (your route definions)
    
        // If you have a catch-all route defined, outcomment is like below
        // {
        //     path: '**',
        //     redirectTo: '/error/not-found',
        // },
    ];
    

    在注释掉您的 catch-all 路由后,路由器不会吞下您的错误,并在您的浏览器控制台中准确显示哪些路由无法与您的定义匹配。

    例如出现如下错误时:

    core.js:4002 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'projects/123'
    Error: Cannot match any routes. URL Segment: 'projects/123'
        at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError (router.js:2459)
    

    您立即知道在您的路由定义中匹配 'projects/123' 存在问题。

    【讨论】:

      【解决方案4】:

      虽然我迟到了。但它可能对 Angular 的新手有用。

      您可以通过两种方式trace angular route changes

      1。 RouterModule (enableTracing)

      您可以将enableTracing 设置为RouterModule,这将记录您所有的路线更改事件。

      RouterModule.forRoot(routes, { 
        enableTracing: true,    /* <-- Set this to true */
      }),
      

      2。订阅Router.events

      如果您不想跟踪所有路由器更改事件,则可以订阅Router.events。您可以使用它过滤特定的路由更改事件。

      constructor(
        private router: Router,
        /* Other dependencies */
      ) {
      
        this.router.events
          .pipe(
            // You can also use traditional if else in the subscribe 
            filter(event => event instanceof NavigationStart)
          )
          .subscribe(event => {
            console.group(`Router event: ${event.constructor.name}`);
            console.log(event);
            console.groupEnd();
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2017-05-29
        • 2018-08-30
        • 1970-01-01
        • 2021-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-24
        相关资源
        最近更新 更多