Typescript Angular2将父服务注入子服务

Typescript Angular2将父服务注入子服务,typescript,angular,Typescript,Angular,假设我有一个简单的app组件: import {Component} from 'angular2/core'; import {bootstrap} from 'angular2/platform/browser'; import {AppComponent} from 'ng2-easy-table/app/app.component'; import {ConfigService} from "./config-service"; @Component({ selecto

假设我有一个简单的
app
组件:

import {Component} from 'angular2/core';
import {bootstrap}    from 'angular2/platform/browser';
import {AppComponent}    from 'ng2-easy-table/app/app.component';
import {ConfigService} from "./config-service";

@Component({
  selector: 'app',
  directives: [AppComponent],
  providers: [ConfigService],
  template: `
    <ng2-table [configuration]="configuration"></ng2-table>
  `
})
export class App {
  constructor(private configuration:ConfigService) {}

}
bootstrap(App, []);
此配置服务:

import {Injectable} from "angular2/core";
@Injectable()
export class ConfigService {
    public searchEnabled = true;
    public orderEnabled = true;
    public globalSearchEnabled = true;
    public footerEnabled = false;
    public paginationEnabled = false;
    public exportEnabled = true;
    public resourceUrl = "http://beta.json-generator.com/api/json/get/E164yBM0l";
}
app
组件中,我将
ng2表放入
组件
ng2 table
app
是根组件,因此我不允许使用
@Input()
(这就是
[configuration]=“configuration”
不起作用的原因(以下是答案)
问题是-如何从
app
组件向
ng2 table
组件注入一些服务,但不使用
@Input()

如何将一些配置传递给我的
ng2表
,或者更简单的是,如何从
node\u modules
初始化组件,该组件需要在构造函数中进行一些配置


这是组件链接:

这一行似乎不正确,因为我猜您想计算
配置
表达式:

<ng2-table [configuration]="'configuration'"></ng2-table>

Angular的依赖项注入将向子组件提供与父组件相同的ConfigService实例,只要它未在子组件的
providers
属性中显式指定。这是由于Angular的分层DI模型,您可以在其例如,
ConfigService
可能不使用DI,因此您可能也希望将其转换为可注入的:
@Input
@Input配置中;
缺少
()
它应该是

@Input() configuration;
我将您的代码复制到Plunker并添加
()
修复了它


为什么不简单地由构造函数注入您在父组件中提供的服务呢

您不需要定义额外的输入来通过配置。这不是一个好的做法

@Component({
  selector: 'app',
  directives: [AppComponent],
  providers: [ConfigService],
  template: `
    <ng2-table></ng2-table>
  `
})
export class App {
  constructor(private configuration:ConfigService) {}

}
@组件({
选择器:“应用程序”,
指令:[AppComponent],
提供者:[配置服务],
模板:`
`
})
导出类应用程序{
构造函数(私有配置:ConfigService){}
}
然后,您可以只注入父提供程序:

  @Component({
  selector: 'ng2-table',
})

export class AppComponent implements OnInit{
  
  constructor(private configuration:ConfigService) {
    console.log("configuration: ", this.configuration); // <-- null
  }

  ngOnInit() {
    console.log("configuration: ", this.configuration); // <-- null
  }
}
@组件({
选择器:“ng2表”,
})
导出类AppComponent实现OnInit{
构造函数(专用配置:ConfigService){

log(“配置:”,this.configuration);//谢谢Thierry的回答,我尝试了你的解决方案,bot仍然没有被传递给子组件。我编辑了一个小问题,因为
控制台。日志
没有显示
未定义的
只显示
null
不客气!你的配置对象在应用组件中不是null吗?我想传递一个任何东西,即使我传递了一些字符串
我得到了
null
非常好的文章,我会读它。在你回答后,我把我的
configService
附加到这个问题上,我认为没问题(希望如此)这是正确的答案,而不是公认的答案。不应使用输入属性注入服务+1@NarendraPathai是的,我认为你是对的。我想你可以通过输入属性注入服务,但这是多余的。我很难弄清楚父组件中的服务实例是如何与子组件共享的s示例:我认为@James Salas的答案比这一个更好,也回答了我上面的问题。但它并没有回答原来的问题。如果James的答案更好的回答了你的问题,当然可以使用它。不,我认为将服务注入到孩子身上是多余的,这个实例已经被共享了。我很难弄清楚如何在本例中,来自父组件的e服务实例与子组件共享:啊,我想@jamessalas回答了我的问题
@Input() configuration;
@Component({
  selector: 'app',
  directives: [AppComponent],
  providers: [ConfigService],
  template: `
    <ng2-table></ng2-table>
  `
})
export class App {
  constructor(private configuration:ConfigService) {}

}
  @Component({
  selector: 'ng2-table',
})

export class AppComponent implements OnInit{
  
  constructor(private configuration:ConfigService) {
    console.log("configuration: ", this.configuration); // <-- null
  }

  ngOnInit() {
    console.log("configuration: ", this.configuration); // <-- null
  }
}