【发布时间】:2021-11-30 00:13:28
【问题描述】:
我创建了我的后端,并且 api 控制器工作正常。 在前端,我已经为我的应用程序创建了模型:
book.model.ts
export class Book {
id : number;
autor: string;
title: string;
returnDate: Date;
numberOfExtensions: number;
cardId:number;
card?: Card;
status?:string;
}
card.model.ts
export class Card {
id : number;
validityDate: Date;
locked: boolean;
userId: number;
user?: User;}
和 user.model.ts
export class User {
id : number;
firstName: string;
lastName: string;
balance: number;}
在前端服务中,我通过以下方式从 api 获取数据:
export class DataService {
constructor(private http: HttpClient) { }
readonly baseURL = "https://localhost:44302/api";
books: Book[];
users: User[];
cards: Card[];
postId: number;
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
async getBooks() {
await this.http.get(this.baseURL + '/books')
.toPromise()
.then(result => this.books = result as Book[]);
}
async getCards() {
await this.http.get(this.baseURL + '/cards')
.toPromise()
.then(result => this.cards = result as Card[]);
}
async getUsers() {
await this.http.get(this.baseURL + '/users')
.toPromise()
.then(result => this.users = result as User[]);
}
async getData() {
await this.getBooks();
await this.getCards();
await this.getUsers();
}
最好先接收所有 SQL 数据吗?
下一步是映射。 例如每本书,我想用合适的ID(伪代码)保存卡片:
book.card = card where book.cardId === card.id
对于每张卡,我都想要卡的所有者-用户:
card.user = user where card.userId === user.id
如果有任何帮助,我将不胜感激! 非常感谢
【问题讨论】:
标签: angular typescript api asp.net-core get