【问题标题】:Access activated route data from some other component从其他组件访问激活的路线数据
【发布时间】:2017-05-27 14:15:33
【问题描述】:

我们有组件(ka-cockpit-panel),它没有映射到任何路线,并手动插入到其他一些组件中,如下所示:

..
...
<section class="ka-cockpit-panel cockpit-1 pull-left">
            <ka-cockpit-panel></ka-cockpit-panel>
</section>
...
..

在这个组件中,我想访问当前活动的路线数据

例如:如果我们有一些其他组件(比如 ka-integration-component )并且它有一些与之关联的路由数据(如下所示),每当我们导航到这个组件时(通过url 或通过单击某些 routerlink ) ,我们想要访问 ka-cockpit-component 中的集成组件路由数据。

 ..
    ... 
    {       
        path: "",       
        component: IntegrationComponent,
        data : {
            cockpit1 : false,
            cockpit2 : true,
            kpi : true
        },  
    }
    ..
    ...

基本上,我们希望为应用中的某些组件配置我们的 ka-cockpit-component,这些组件映射到某个路由,以便我们可以隐藏/显示或更改其外观。



座舱组件代码:

import { Component, OnInit } from '@angular/core';
import { Router,Event,NavigationEnd,ActivatedRoute } from '@angular/router';

@Component({
    selector: 'ka-cockpit-panel',
    templateUrl: './cockpit-panel.component.html',
    styleUrls : ['./cockpit-panel.component.scss']
})
export class CockpitPanelComponent implements OnInit {

    constructor(private router:Router,private activatedRoute : ActivatedRoute) {
         this.router.events.subscribe( (event:Event) => {
            if(event instanceof NavigationEnd) {
                console.log("Cockpit Panel Component : Route successfully changed -  ",event,this.router,this.activatedRoute);

                  // THIS IS WHAT WE WANT - get  Integration component route data here whenever i navigate to integration component!!!

            }
        });
     }

    ngOnInit() { }
}

【问题讨论】:

标签: javascript angular typescript angular2-routing


【解决方案1】:

你必须使用Resolve Guard 来实现你想要实现的东西。

// MyDataResolver 服务

import { Injectable }             from '@angular/core';
import { Router, Resolve, RouterStateSnapshot,
         ActivatedRouteSnapshot } from '@angular/router';

@Injectable()
export class MyDataResolver implements Resolve<any> {
  constructor(private cs: CrisisService, private router: Router) {}
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<any> {

    let pathFromRoot = route.pathFromRoot;

    // you can compare pathFromRoot with your route to return different data

    return Promise.resolve({
        cockpit1 : false,
        cockpit2 : true,
        kpi : true
    });

  }
}

//路由配置

.
.
{       
    path: "",       
    component: IntegrationComponent,
    resolve : {
        data: MyDataResolver
    },  
}
.
.

// 组件

export class CockpitPanelComponent implements OnInit {
  someBinding : string = "testing Value";

  constructor(private router:Router,private activatedRoute : ActivatedRoute) {

    this.activatedRoute.data.subscribe( (res) => {

      // here you will get your data from resolve guard.
      console.log(res);

    });
  }

  ngOnInit() { }
}

【讨论】:

  • 这不起作用。 "this.activatedRoute" 有 ka-cockpit-panel-component Parent 的路由信息​​,而不是集成组件。
  • 你检查过this.activatedRoute.parent,它是路由器状态树中当前激活路由的父级,所以我想检查this.activatedRoute.parent.data.subscribe((res) =&gt; { console.log(res); })
  • 类型注释Promise &lt;any&gt; 是一个糟糕的选择。完全不使用它,你会得到更好的类型。
猜你喜欢
  • 1970-01-01
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
相关资源
最近更新 更多