angular - How can I get route name from app.component? -


in app.component.ts have code below route name, /. know why? expect example home, contact.

import { component } '@angular/core'; import { router } '@angular/router';  @component({   selector: 'app-root',   templateurl: './app.component.html',   styleurls: ['./app.component.scss'] }) export class appcomponent {    constructor(private router: router) {     console.log(router.url);   } } 

the appcomponent initialized, , constructor executed, when application loaded. hence reason shows "/". navigate other routes, appcomponent remains loaded , constructor not executed again.

if provide more information on trying accomplish, may able provide alternative suggestion you.

if want watch routes , obtain url, can use observable here instead. this:

this.router.events.subscribe((event: event) => {     if (event instanceof navigationend) {         console.log((<navigationend>event).url);     } }); 

or if want debugging purposes, can enable tracing so:

routermodule.forroot([     { path: 'welcome', component: welcomecomponent },     { path: '', redirectto: 'welcome', pathmatch: 'full' },     { path: '**', component: pagenotfoundcomponent } ], { enabletracing: true }) 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -