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:
In router_module.ts::setupRouter:
if (opts.enableTracing) {
to
router.tracing = opts.enableTracing</pre>
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.
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.
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.
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)
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With