因为我是Angular的新手,任何人都可以使用angular 2来提供一个简单的解决方案来加载JSON文件数据.
我的代码如下
的index.html
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
app.component.ts
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div id="main">
Main Div
<div id = "header"></div>
<div id …Run Code Online (Sandbox Code Playgroud)我的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)