根据类似问题的之前回答,如这个,从资产目录下载或者读取文件应该是使用 HttpClient
的 get
方法直接调用。例如,如果我有一个 DownloadService
,我可以简单地定义它为:
export class DownloadService {
constructor(http: HttpClient){}
download(data) {
this.http.get('/assets/path/to/file.ext').subscribe(res => process(res));
}
}
然而,当我使用ng serve
测试时,我收到一个错误提示,说/assets/path/to/file.ext
不是有效的URL。我尝试过./assets/path/to/file.ext
和assets/path/to/file.ext
,但我得到了相同的错误。我需要在运行时配置一些东西才能使其正常工作,还是自从那些答案被写出以来就已经改变了?我正在使用 Angular 9。