在angular2最新的路由器中通过url获取组件

时间:2016-08-03 11:17:51

标签: angular angular2-routing

使用以前的angular2路由器, 我能够通过网址获取组件名称。

            console.log(this.router.instruction);
            console.log(this.router.currentInstruction.component);
            console.log(this.router.currentInstruction.component.routeName);

但是使用新的路由器我无法做到。 还有其他选择吗?

我想跟踪我的应用中的每个路由,然后使用它的组件名称(不是网址值)记录

constructor(private router: Router,
            private cookieService: CookieService,
            private adminService: AdminService,
            private appService: AppService) {
    router.events.subscribe((e: Event) => {
        if (e instanceof NavigationEnd) {

            //
            // WANT TO GET COMPONENT NAME by this.router.url
            //

            let newRoute = this.router.url || '/';
            if (this.currentRoute != newRoute) {
                console.log('route to ', newRoute);
                this.currentRoute = newRoute;
            }
        }
    });
}

任何机会?

1 个答案:

答案 0 :(得分:0)

您可以访问有效路线,如下面的代码段

constructor(private router:Router, private currentActivatedRoute:ActivatedRoute)
ngOnInit()
{
var comp = this.currentActivatedRoute.component
}

如果您需要在父组件see

中访问子激活路线

ActivatedRoute