gpt4 book ai didi

node.js - 我怎样才能在 Angular 问题中获得一条记录?

转载 作者:行者123 更新时间:2023-12-03 12:17:43 24 4
gpt4 key购买 nike

我要获取数据库的一条记录,现在我犯了一个小错误。不知道怎么才能上记录。我已经成功地从我的后端获取了记录,并且我有 console.log 来显示我得到的值。让我分享我的代码。

我有两个页面,第一页是显示所有书籍的首页,这里显示所有书籍没有问题

问题出在这里,在我的书的详细信息页面中。我不知道如何在单击书的书名时只显示一本书,我已经看到我得到了后端响应的值,但我不知道如何在我的书的详细信息页面上显示它。我不知道 getOneBook 订阅

书籍详细信息 html

<h1>Book-detail</h1>
<div *ngIf="book" class="bookdetail-block">
<div *ngFor="let bookdetail of book" class="bookdetail">
<h1>{{bookdetail.title}}</h1>
<p><img [src]="bookdetail.image" ></p>
<p>{{bookdetail.author}}</p>
<p>{{bookdetail.price}}</p>
<p>{{bookdetail.isbn}}</p>
<p>{{bookdetail.description}}</p>

</div>
</div>

书籍细节 ts

    export class BookDetailComponent implements OnInit {
isbn: any;
book: any;

constructor(private data: DataService, private router: Router, private route: ActivatedRoute) { }

ngOnInit() {

this.route.snapshot.paramMap.get("isbn");

this.data.getOneBook(this.isbn).subscribe(data =>{
console.log({ data }); //show data
this.book = data
//console.log(this.books);
})
}

getBookDetail(books) {
this.data.getOneBook(books.isbn); //isbn of book
}
}

错误在这里

 ERROR in src/app/bookdetail/bookdetail.component.ts(16,35): error TS2551: Property 'subscribe' does not exist on type 'Subscription'. Did you mean 'unsubscribe'?

这是为了获取后端的响应,请看图片 data.service

主页 html

<h1>All Books</h1>
<ul *ngIf="books" class="info">
<li *ngFor="let book of books">
<p><img [src]="book.image" class="bookimg"></p>

<a routerLink="/bookdetail/{{book.isbn}}" (click)="getBookisbn(book)"><h3>{{ book.title }}</h3></a>
<p>{{ "By "+ book.author }}</p>
<span class="price-block" >{{ "HKD$ " + book.price}}</span>

<div class="btn-fav">
<p>
<button>Add to favorite</button>
</p>
</div>
<div class="btn-cart">
<p>
<button>Add to cart</button>
</p>
</div>
</li>
</ul>

首页

export class HomeComponent implements OnInit {

h1Style: boolean = false;
books: Object;

constructor(private data: DataService) {}

ngOnInit() {
this.data.getBooks().subscribe(data=> {
console.log({data}); //show data
this.books = data
//console.log(this.books);
})
}

// object of book
getBookisbn(book) {
this.data.getOneBook(book.isbn) //isbn of book
//this.router.navigateByUrl("bookdetail/" + book.isbn)
}

数据.服务.ts

export class DataService {


constructor(private http: HttpClient) { }

getBooks() {
return this.http.get('http://localhost:10888/book');
}

getOneBook(isbn:any) {
// console.log("=============" + isbn )
return this.http.get('http://localhost:10888/bookdetail/?isbn=' + isbn).subscribe(
(val) => { // Get has no error and response has body
console.log("Get successful value returned in body", val);
},
response => {
console.log("Get call in error", response);
},
() => { // Get has no error, response has no body
console.log("The Get observable is now completed.");
});
}

最佳答案

根据您的服务:

getOneBook 接受 1 个参数,即 isbn

所以当你调用它(订阅)时你应该使用

this.data.getOneBook(this.isbn)

而不是

this.data.getOneBook(this.books)

我假设您将 isbn 作为 参数 包含在激活的路由中,因此在这种情况下,您可以尝试这样的代码:

//import { ActivatedRoute, Router } from "@angular/router";  

export class BookDetailComponent implements OnInit {
isbn: number;
book: any;

constructor(private data: DataService,
private router: Router,
private route: ActivatedRoute
) {}

ngOnInit() {

this.isbn = this.route.snapshot.paramMap.get("isbn");

this.data.getOneBook(this.isbn).subscribe(data =>{
this.book = data
})
}
}

请注意我也将 this.books 替换为 this.book

在您的主页组件 HTML 中

替换

<a routerLink="/bookdetail/{{book.isbn}}" (click)="getBookisbn(book)"><h3>{{ book.title }}</h3></a>

<a [routerLink]="['/bookdetail/', book.isbn]"><h3>{{ book.title }}</h3></a>

关于node.js - 我怎样才能在 Angular 问题中获得一条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65328543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com