在处理我面临的问题的程序时,构造函数及其子类的依赖注入。
constructor(private url: string, private http: Http) { }
Run Code Online (Sandbox Code Playgroud)
constructor(http: Http) {
super('https://jsonplaceholder.typicode.com/posts', http);
}
Run Code Online (Sandbox Code Playgroud)
在此重构之前(移动所有常见的 CRUD 操作并通过子类扩展它),我的代码按预期工作,但在进行上述更改后,我收到以下错误:
Date: 2020-03-22T15:26:23.248Z - Hash: 7130497a38c152c58258
5 unchanged chunks
Time: 1859ms
ERROR in src/app/services/data.service.ts:14:23 - error NG2003: No suitable injection token for parameter 'url' of class 'DataService'.
Found string
14 constructor(private url: string, private http: Http) { }
Run Code Online (Sandbox Code Playgroud)
此外,当我从 Datsource 构造函数中删除url参数(相应地修改 PostService.ts)时,api 按预期工作。 …