Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable route tracing programmatically in Angular 2?

I know I can enableTracing on the Angular 2 router:

export const routing: ModuleWithProviders = 
    RouterModule.forRoot(routes, { enableTracing: true });

Is there any way to programatically set that value?

I have a config.json file that has a number of application settings. I would like to include a boolean property that can be used to control whether tracing is on or not. I'd rather not have the customer have the ability to modify the file that contains my routes, but still have the ability to turn on tracing to help debug edge cases that didn't get caught by tests.

I'm OK with having to restart the application, but not OK with having to rebuild.

[Update] After looking at the router source, it doesn't look do-able without a pull request. Simple explanation of what the change would need to be:

  1. In router.ts, add a public property called tracing: boolean
  2. In router_module.ts::setupRouter:

    • Change
      if (opts.enableTracing) {
      

      to

      router.tracing = opts.enableTracing</pre>
      

    • Change the observable:
      router.events.subscribe(e => { ...
      

      to

      router.events
         .filter(e => router.tracing)
         .subscribe(e => { ...</li>
      

3. Probably need to add some validation on the tracing property.

With these changes, one could import Router and then set the router.tracing property to turn it on and off.

I have no idea what the performance difference is between emitting all of the events with no subscriber and emitting all of the events with a filtered subscription.

like image 979
Nick Avatar asked Jan 14 '17 00:01

Nick


People also ask

How do I enable Angular routing?

Add the AppRoutingModule link The router is dedicated to routing and imported by the root AppModule . By convention, the module class name is AppRoutingModule and it belongs in the app-routing.module.ts in the src/app directory. Run ng generate to create the application routing module.

What is the use of enable tracing in router module?

The very first thing we can do during development to start troubleshooting router-related issues is to enable tracing, which will print out every single event in the console.


1 Answers

For now there is no explicit way to do it programmatically. Our workaround is to enable it only for local development so that we can get all the details/exception-stacktrace etc. Something like:

let routes = [{path: ..., component : ...}, ...];
RouterModule.forRoot(routes, {
                       enableTracing: /localhost/.test(document.location.host)
                     });
like image 70
LeOn - Han Li Avatar answered Oct 01 '22 06:10

LeOn - Han Li