gpt4 book ai didi

angular - 访问路由参数 - angular 8

转载 作者:行者123 更新时间:2023-12-05 09:11:31 24 4
gpt4 key购买 nike

嘿,我正在尝试使用产品构建网站,但我无法从 productsComponent 获取数据到 ProductDetailsComponent。请帮助我。

在我的 Product.ts 中我有:

    export class Product {
id: number;
name: string;
constructor(id: number, name: string) {
this.id = id;
this.name = name;
}
}

在我的 ProductsComponenet 中我有:

    import { Component } from '@angular/core';
import { Product } from '../services.service';

@Component({
selector: 'app-services',
templateUrl: './services.component.html',
styleUrls: ['./services.component.css']
})
export class ServicesComponent {
public products: Product[] = [
new Product(1, "Product 001"),
new Product(2, "Product 002"),
new Product(3, "Product 003"),
new Product(4, "Product 004"),
new Product(5, "Product 005"),
new Product(6, "Product 006"),
new Product(7, "Product 007"),
new Product(8, "Product 008")
];
}

在我的 ProductDetailComponent 中:

    import {Component, OnInit} from '@angular/core';
import { ActivatedRoute} from '@angular/router';
import { Product } from '../services.service';



@Component({
selector: 'app-detail-service',
templateUrl: './detail-service.component.html',
styleUrls: ['./detail-service.component.css']
})
export class DetailServiceComponent implements OnInit {

public products: Product[] = [
new Product(1, "Product 001"),
new Product(2, "Product 002"),
new Product(3, "Product 003"),
new Product(4, "Product 004"),
new Product(5, "Product 005"),
new Product(6, "Product 006"),
new Product(7, "Product 007"),
new Product(8, "Product 008")
];
product: Product = this.products[0];

constructor(private routes: ActivatedRoute) {}
ngOnInit() {
this.routes.params.subscribe(params => {
this.products.forEach((p: Product) => {
if (p.id === params.id) {
this.product = p;
}
});
});
}
}

我在 products.html 中显示每个产品,在我点击任何产品后,它会呈现我的 productdetails.html 和我的产品,但不显示正确的名称和 ID。 IT 在每个产品中显示第一个,但链接与产品的 id 是正确的。请帮忙。我真的搞不懂这是怎么回事。

最佳答案

要直接解决您的问题,您需要执行以下操作:

this.route.paramMap.subscribe(params => {
let id = params.get('id');
this.product = this.products.find(product => product.id === +id);
})

请注意,路由参数始终是字符串,因此在上面的代码片段中,我在 find 方法中将 id 转换为数字。

不过,我建议您完全避免显式订阅。下面,我描述了一个更加可维护的实现,它更紧密地遵循响应式(Reactive)范例,并且我已经包含了一个有效的 stackblitz 示例。

您将通过管道连接到 paramMap observable 以获取您的路由参数,使用 switchMap 运算符订阅它,并使用产品服务通过 id 获取产品。

您的 product-detail.component.ts 看起来像这样:

@Component({
selector: 'app-product-detail',
templateUrl: './product-detail.component.html',
styleUrls: ['./product-detail.component.css']
})
export class ProductDetailComponent implements OnInit {

product$: Observable<any>;

constructor(
private route: ActivatedRoute,
private productService: ProductService
) { }

ngOnInit() {
this.product$ = this.route.paramMap.pipe(
switchMap(params => {
let id = params.get('id');
return this.productService.getProductById(+id);
})
)
}

}

请注意,pipe 在我们订阅它之前不会执行。为此,我们应该使用 async 管道。所以你的 product-detail.component.html 应该是这样的:

<div class="product-detail-wrapper" *ngIf="product$ | async as product">
<h3>Product Detail</h3>
<p>Product ID: {{ product.id }}</p>
<p>Product Name: {{ product.name }}</p>
</div>

有关工作示例,请查看此 stackblitz .

关于angular - 访问路由参数 - angular 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59938409/

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