![](/img/trans.png)
[英]How to show and get billions of data from lumen api in angular 8
[英]How to get an array of data from a php API ( laravel lumen) in Angular
我被困住了,因为这是我第一次使用 Angular,我可能不了解一些基础知识。
我已经制作了一个 PHP API ,它从 MySql 数据库返回数据,我使用 Http18B8624A0F5F7221FCDA7 在 ZD18B8624A0F5F7221FCDB7 中获取它们。
对数据库的调用是
public function showOneCoursBy($id){
$results = DB::select("select * from cours where id_cours = ".$id);
return $results;
}
访问我得到的 API 页面
[{"id_cours":1,"id_exercice":1,"titre":"XXX","description":"XXX","contenu":"XXX","mediaPath":"XXX","dateDebut":"0000-00-00","dateFin":"0000-00-00"}]
所以看起来是对的。
在 Angular 中,我得到了数据
constructor(
private firestore: AngularFirestore,
private http: HttpClient,
public db: AngularFirestore
) {
this.http.get('http://localhost:3000/cours/').
toPromise().then(data => {
console.log(Object.keys(data).map(key => ({type: key, value: data[key]})));
}
);
}
在服务中,console.log 确实返回
(2) […]
0: {…}
type: "0"
value: Object { id_cours: 1, id_exercice: 1, titre: "Premier Cours", … }
<prototype>: Object { … }
1: {…}
type: "1"
value: Object { id_cours: 2, id_exercice: 2, titre: "Cours n°2", … }
<prototype>: Object { … }
length: 2
<prototype>: [
但是我怎样才能只将这些元素中的一个作为数组呢? 还是只访问一个属性?
先谢谢了
您可以简单地为 Object 声明一个接口
export interface Exercice {
id_cours: number;
id_exercice: number;
titre: string;
description: string;
contenu: string;
mediaPath: string;
dateDebut: string;
dateFin: string
}
您可以通过将您的响应键入为“练习”来访问数据
this.http.get('http://localhost:3000/cours/')
.pipe(take(1))
.subscrible((response: Exercice[]) => {
response.forEach(exercice => console.log(exercice.titre));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.