【问题标题】:Angular - error TS2554: Expected 0 arguments, but got 3Angular - 错误 TS2554:预期 0 个参数,但得到 3 个
【发布时间】:2018-11-28 15:54:05
【问题描述】:

我遇到以下错误:

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();
      }));
    });

更新

根据下面 cmets 的要求。

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) {

                    }
    }

【问题讨论】:

  • app.service.ts 文件抛出的错误。您需要共享 app.service.ts 文件
  • 与我们分享您的Coffee 类,您将3 个参数传递给构造函数,但您的Coffee 可能没有构造函数来接受3 个参数。
  • 刚刚添加了 2 个课程(咖啡和地点)提前感谢您的帮助!
  • 你在使用 angular cli 吗?如果是这样,您可能需要重新运行ng serve
  • 使用 angular cli,当我运行 build 或 serve 时,我得到了错误。我也尝试过清理,但似乎也没有帮助

标签: angular typescript


【解决方案1】:

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

import {Coffee} from '<relative path>'

尝试从正确的路径导入 Coffee。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2020-05-31
    相关资源
    最近更新 更多