Angular 2 - 使用静态变量将参数传递到组件路径(而不是单独的服务)

时间:2018-03-21 14:19:58

标签: angular routing components

我想启动一个路径,并传入一个丰富的对象,它将描述它应该做什么。

我开始实施服务方法(我收集的是最佳实践)。

然后我很快就尝试在我导航到的组件上使用静态属性,并且运行正常。

那么,这是否真的令人不悦?

拥有单独的服务来管理数据,看起来有点像行李(在这种情况下)。

源组件

import { TargetComponent } from "...";
import { Router } from '@angular/router';
import { TargetComponentConfig } from '...';

var o = new richObject();
....
TargetComponent.Parameter = o;

this.router.navigate(['/routeForTargetComponent']); 

目标组件

public static config:TargetComponentConfig;

ngOnInit(){

if (config != null) {
    //Make use of the config to set things up
}

0 个答案:

没有答案