routing - Angular router url returns slash -
i'm trying current router path using router, when console.log(this.router.url) returns "/", although i'm on "/login". when i'm consoling entire this.router object, there property url has value "/login".
here code app.component.ts
export class appcomponent implements oninit{ constructor(private router: router) {} ngoninit(){ console.log(this.router); } } app.module.routing.ts
import {ngmodule} '@angular/core'; import {preloadallmodules, routermodule, routes} '@angular/router'; import {notfoundcomponent} './not-found/not-found.component'; import {authguard} './auth/auth-guard.service'; const approutes: routes = [ { path: '', loadchildren: './first/first.module#firstmodule'}, { path: 'login', loadchildren: './login/login.module#loginmodule'}, { path: '404', component: notfoundcomponent, canactivate: [authguard] }, { path: '**', redirectto: '/404'} ]; @ngmodule({ imports: [routermodule.forroot(approutes, {preloadingstrategy: preloadallmodules})], exports: [routermodule] }) export class appmodulerouting {} and firstmodule routing:
import {ngmodule} '@angular/core'; import {routermodule, routes} '@angular/router'; import {firstcomponent} './first.component'; import {authguard} '../auth/auth-guard.service'; const firstroutes: routes = [ { path: '', component: firstcomponent, canactivate: [authguard], children:[ { path: '', loadchildren: './content-container/container.module#containermodule' } ]} ]; @ngmodule({ imports: [routermodule.forchild(firstroutes)], exports: [routermodule] }) export class firstroutes {}
type 1.we can use window.location.pathname
type 2.constructor(router: router) { router.events.subscribe((url:any) => console.log(url)); console.log(router.url); // print path eg:"/login" }
Comments
Post a Comment