文件代碼css
{ "**": { "target": "http://localhost:8000", // 指向須要代理的api地址 "secure":false } }
ng serve --proxy-config proxy.conf.json
1
這裏寫圖片描述html
> const express = require("express"); > > const app = express(); > > let dataSet = [ > {"id":"0","name":"張三","age":20}, > {"id":"1","name":"李四","age":34}, > {"id":"2","name":"王五","age":30}, > {"id":"3","name":"馬六","age":50} ]; app.get("/products",(req,res)=>{ > res.json(dataSet); }); > > app.listen(8000,"localhost",()=>console.log("服務已經啓動"))
//ts文件 import {Component, OnInit} from '@angular/core'; import {Observable} from "rxjs/Observable"; import {Http} from "@angular/http"; import "rxjs/Rx"; @Component({ selector: 'app-httpdemo', templateUrl: './httpdemo.component.html', styleUrls: ['./httpdemo.component.css'] }) export class HttpdemoComponent implements OnInit { dataSource: Observable<any>; dataSet: Array<any> = []; constructor(private http: Http) { this.dataSource = this.http.get("/products").map((res) => res.json()); } ngOnInit() { this.dataSource.subscribe( (data) => this.dataSet = data ) } } //html代碼 <ul> <li *ngFor="let item of dataSet">{{item.name}}--{{item.age}}</li> </ul>