角度-错误TS2554:应使用0个参数,但得到3个

时间:2018-11-28 03:57:11

标签: angular typescript

我遇到以下错误:

src/app/data.service.ts(11,7): error TS2554: Expected 0 arguments, but got 3.
src/app/data.service.ts(12,7): error TS2554: Expected 0 arguments, but got 3.

我是Angular的新手,我的代码似乎与我正在关注的教程相同。

我的代码可以在下面看到

data.service.ts:

    import { Injectable } from '@angular/core';

    @Injectable()
    export class DataService {

    constructor() { }

      getList(callback) {
        //TODO: Change it with a real Web Service    
        const list = [
          new Coffee("Double Espresso", "Sunny Cafe", new PlaceLocation("123 Market St", "San Francisco")),
          new Coffee("Caramel Americano", "Starcoffee", new PlaceLocation("Gran Via 34", "Madrid"))
        ];
        callback(list);
      }

      save(coffee, callback) {
        //TODO: Change it with a real Web Service
        callback(true);
      }

    }

data.service.spec.ts:

    import { TestBed, inject } from '@angular/core/testing';

    import { DataService } from './data.service';

    describe('DataService', () => {
      beforeEach(() => {
        TestBed.configureTestingModule({
          providers: [DataService]
        });
      });

      it('should be created', inject([DataService], (service: DataService) => {
        expect(service).toBeTruthy();
      }));
    });

更新

按照以下评论的要求。

Coffee.ts:

    class Coffee {

        // Properties
        type: string;
        rating: number;
        notes: string;
        tastingRating: TastingRating;

        constructor(public name: string, public place: string, public location: PlaceLocation) {

        }
    }

如果需要 PlaceLocation.ts

    class PlaceLocation {

        constructor(public address: string = "", 
                    public city: string = "",
                    public latitude: number = null,
                    public longitude: number = null) {

                    }
    }

1 个答案:

答案 0 :(得分:0)

在从现有类创建新对象之前,我想您要将其导入组件中。

import {Coffee} from '<relative path>'

尝试从正确的路径导入咖啡。