【问题标题】:Angular 2 router pass parameters and get them when changesAngular 2路由器传递参数并在更改时获取它们
【发布时间】:2016-05-15 22:21:24
【问题描述】:

如何将一些参数传递给我的 rotes,就像在旧路由器中一样?

@RouteConfig([
  new Route({path: '/home', component: HomeCmp, name: 'HomeCmp' })
])

新的

@Routes([
  {path: '/login', component: Component}
])

然后让他们订阅更改?

 export class AppComponent {

        public constructor(private router: Router) {

            this.router.changes.subscribe((x) => {
                console.log(x);
            });

我的意思是在旧的角度有一个状态,我们可以附加事件onBefore 等并获取当前状态。实际上这将如何在这里工作。 this.router.changes.subscribe - 它正在触发更改,但不要提供任何相关信息 - x 未定义。

【问题讨论】:

    标签: parameters angular onchange router


    【解决方案1】:

    订阅中不再存在参数。您需要使用定位服务查看当前路线:

    import {Location} from '@angular/common';
    import {Router} from '@angular/router'
    
    export class SomeComponent {
      constructor(router:Router, private location:Location) {
        router.changes.subscribe(() => this.routeChanged());
      }
    
      private routeChanged():void {
        var path:string = this.location.path();
        console.log("Path:" + path);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 2016-10-28
      • 2016-09-21
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 2017-08-17
      • 2017-01-16
      • 1970-01-01
      相关资源
      最近更新 更多