Angular 2获取当前路线 [英] Angular 2 Get current route
本文介绍了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屋!
查看全文