【发布时间】: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