Angular 2 TypeScript错误:无法解析ProductsService的所有参数

时间:2017-01-31 20:23:08

标签: angular typescript kendo-ui kendo-ui-angular2

我收到以下内容:“错误:无法解析ProductsService的所有参数”。

参考:http://www.telerik.com/kendo-angular-ui/components/dropdowns/dropdownlist/#toc-data-binding

product.model.ts

export class Product {
constructor(
    public ProductID?: number,
    public ProductName?: string,
    public UnitPrice?: number,
    public UnitsInStock?: number,
    public Discontinued?: boolean
) { } }

products.service.ts

import { Injectable } from '@angular/core';
import { Jsonp, JsonpModule } from '@angular/http';
import { Product } from './product.model';
import { Observable } from 'rxjs/Rx';

@Injectable()

// See: http://www.telerik.com/kendo-angular-ui/components/dropdowns/dropdownlist/#toc-data-binding

export class ProductsService {
    constructor( private jsonp: Jsonp) { }

    fetchData(action: string = "", data?: Product): Observable<Product[]>{
        console.log("BGW Action: " + action);
        console.log("BGW this.serializeModels(data) = " + this.serializeModels(data));
        return this.jsonp.get(`http://demos.telerik.com/kendo-ui/service/Products/${action}?callback=JSONP_CALLBACK${this.serializeModels(data)}`).map(response => response.json())
    }

    private serializeModels(data?: Product): string {
        return data ? `&models=${JSON.stringify([data])}` : '';
    }
}

app.component.ts

包含:

import { ProductsService } from './products.service';
import { Product } from './product.model';
import { Inject } from '@angular/core';

providers: [ProductsService],

public productsData: Array<Product> = [];
// public listItems: Array<string> = [];

//constructor (@Inject(ProductsService) private productService: ProductsService) { }
constructor (private productService: ProductsService) { }

ngOnInit() {
  this.productService.fetchData().subscribe(
    (data) => this.productsData = data
  )
}

任何想法都非常感激。

此致

1 个答案:

答案 0 :(得分:3)

要解决此问题,请在 app.component.ts 中更改:

constructor (private productsService: ProductsService) { }

constructor (@Inject(ProductsService) private productsService: ProductsService) { }

product.service.ts 中,更改:

constructor( private jsonp: Jsonp) { }

为:

constructor(@Inject(Jsonp) private jsonp: Jsonp) { }