首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Angular2监视路由更改

Angular2监视路由更改
EN

Stack Overflow用户
提问于 2015-12-24 06:40:33
回答 5查看 28.1K关注 0票数 26
@RouteConfig([
    {path: '/about', name: 'About', component: About, useAsDefault: true},
    {path: '/test', name: 'Test', component: Test}
])

export class MyApp {
    router: Router;
    location: Location;
    isCollapsed: boolean = true;

    // ON ROUTE CHANGE {
        this.isCollapsed = true;
    // }

    constructor(router: Router, location: Location) {
        this.router = router;
        this.location = location;
    }
}

我需要在每次路径改变时改变变量值,如何在Angular 2.x中观察这个事件?

EN

回答 5

Stack Overflow用户

发布于 2016-07-18 19:13:39

如果您正在使用

"@angular/router": "3.0.0-alpha.7", 
"@angular/router-deprecated": "2.0.0-rc.2",

然后

this.router.events.subscribe((event) => {
      console.log('route changed');
});
票数 20
EN

Stack Overflow用户

发布于 2015-12-24 06:48:36

以下是我在应用程序中使用的内容。您可以订阅Route实例来跟踪更改。

class MyClass {
  constructor(private router: Router) {
    router.subscribe((val) => /*detect changes*/)
  }
}
票数 17
EN

Stack Overflow用户

发布于 2018-03-27 14:02:43

事件是可观察的,所以你可以订阅它,我已经在angular5中成功地尝试过了

this.router.events.subscribe((event) => {
console.log(event);
 if(event['url'] && event['url'] == '/') {
    console.log('Home page');
    //any other functionality you can do here
 }
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34444822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档