Angular 2 获取当前路线 [英] Angular 2 Get current route

查看:38
本文介绍了Angular 2 获取当前路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要以某种方式检查我是否在主页上并执行某些操作,而在其他页面中则不这样做.此外,所有页面上都导入了该组件.

So I need somehow check if I am on home page and do something and in other pages don't do that. Also that component imported on all of pages.

如果我在主页上,如何检测该组件???

How can I detect on that component if I'm on home page???

谢谢

推荐答案

试试这个,

import { Router } from '@angular/router';
export class MyComponent implements OnInit {

    constructor(private router:Router) { ... }

    ngOnInit() {
        let currentUrl = this.router.url; /// this will give you current url

        // your logic to know if its my home page.
    }

}

这篇关于Angular 2 获取当前路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆