Angular2,在组件中按需注入服务

时间:2017-05-25 07:14:53

标签: angular dependency-injection

这是我目前的组成部分。

import { Component, OnInit, Input } from '@angular/core';
import { SaleInvoiceService } from "../../services/sale-invoice.service";
import { SaleInvoice } from "../../modules/sales/sale-invoice/sale-invoice";

@Component({
  selector: 'fs-list',
  templateUrl: 'fs-list.component.html',
  styleUrls: ['fs-list.component.scss']
})
export class FSListComponent implements OnInit {
  @Input() config: any;

  data: SaleInvoice[];
  constructor(private service: SaleInvoiceService) {}

  ngOnInit() {
    this.service.getAll()
      .subscribe(data => {
        this.result.data = data;
      });
  }
}

我需要根据fs-list组件输入值中提到的服务名称注入customerService,SupplierService,SaleInvoiceService等服务。有没有办法按需注入服务,而不是最初在构造函数

0 个答案:

没有答案