我的Angular 4项目目录中有一个.txt文件,我想阅读其内容.怎么做 ?以下是我使用的代码.
该文件位于"app"文件夹中的"files"文件夹中.我有HTTPClient代码的组件位于'httpclient'文件夹中,该文件夹位于'app'文件夹中.
含义'files'文件夹和'httpclient'文件夹是子文件夹.
代码如下所示.它没有工作因为我得到404错误 - 'GET http:// localhost:4200/files/1.txt 404(Not Found)'
this.http.get('/files/1.txt').subscribe(data => {
console.log(data);
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
// A client-side or network error occurred. Handle it accordingly.
console.log('An error occurred:', err.error.message);
} else {
// The backend returned an unsuccessful response code.
// The response body may contain clues as to what went wrong,
console.log(`Backend returned code ${err.status}, body was: ${err.error}`);
}
}
);
Run Code Online (Sandbox Code Playgroud)