我正在尝试将记录器实现为sqlite。
ilogger.ts
export interface ILogger {
write (log: Log): any;
}
ilogger.service.ts
export interface ILoggerService {
info(log: Log): any;
error(log: Log): any;
}
irepository.ts
export interface IRepository<T> {
find(key: number): any;
insert(data: T): any;
remove(key: number): any;
fetch(count: number, skip: number): any;
}
sqlite.logger.impl.ts
export class SqliteLoggerImpl implements ILogger {
sqliteRepo: IRepository<Log>;
SqliteLoggerImpl(sqliteRepo: IRepository<Log>) {
this.sqliteRepo = sqliteRepo;
}
write(log: Log): any {
return this.sqliteRepo.insert(log);
}
}
logger.service.impl.ts
export class LoggerServiceImpl implements ILoggerService {
logger: ILogger;
LoggerServiceImpl(logger: ILogger) {
this.logger = logger;
}
info(log: Log): any {
log.severity = 0;
return this.logger.write(log);
}
error(log: Log): any {
log.severity = 1;
return this.logger.write(log);
}
}
app.component.ts
@Component({
selector: "my-app",
templateUrl: "app.component.html"
})
export class AppComponent {
logs: Array<Log>;
loggerService: ILoggerService;
constructor() {
let repo: IRepository<Log> = new SqliteRepositoryImpl();
// [ts] Supplied parameters do not match any signature of call target
let logger: ILogger = new SqliteLoggerImpl(repo);
this.loggerService = new LoggerServiceImpl(logger);
}
}
我不明白这两行:
let logger: ILogger = new SqliteLoggerImpl(repo);
this.loggerService = new LoggerServiceImpl(logger);
SqliteLoggerImpl构造函数需要一个IRepository,我已经给它了。 LoggerServiceImpl构造函数需要一个ILogger,我已经给它了。
为什么我仍然看到这个,“[ts]提供的参数与呼叫目标的任何签名都不匹配”?
答案 0 :(得分:1)
TypeScript不像其他语言,其中构造函数是类的名称。您需要使用关键字constructor
。你在所有实现中拥有的只是类中与类同名的方法。使用new
时,此方法不。这是constructor
。当我们没有定义一个时,每个类都有一个默认的无参数构造函数。因此,当您尝试执行new Something(someArg)
时,这将失败,因为默认的no-arg构造函数不会接受参数。所以只需改变所有你想要的构造函数&#34;到constructor
s