Angular细节组件。根据URL仅显示数组的特定元素。
创始人
2024-10-30 09:30:10
0

我们可以使用ActivatedRoute服务中的查询参数来实现此目的。 先在app-routing.module.ts中定义路由:

{ path: 'products/:id', component: ProductDetailComponent }

然后在ProductDetailComponent中,我们可以注入ActivatedRoute并使用其params属性来获取路由参数。

import { ActivatedRoute } from '@angular/router';

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

productId: number; products: any = [ { id: 1, name: 'Product 1', category: 'Category 1' }, { id: 2, name: 'Product 2', category: 'Category 2' }, { id: 3, name: 'Product 3', category: 'Category 1' }, { id: 4, name: 'Product 4', category: 'Category 2' } ];

constructor(private route: ActivatedRoute) { }

ngOnInit() { this.route.params.subscribe(params => { this.productId = +params['id']; }); } }

在template中,我们可以使用*ngIf指令来根据productId显示对应的products数组元素。

Product Details

Product 1 Details

Product 2 Details

Product 3 Details

Product 4 Details

如果我们有很多元素需要显示,可以使用ngSwitch结构来替换多个*ngIf。

Product 1 Details
Product 2 Details
Product 3 Details
Product 4 Details

相关内容

热门资讯

辅助透视"wepok... 辅助透视"wepoker私人局可以透视"详细辅助力荐教程(固有有挂);人气非常高,ai更新快且高清可...
透视app"菠萝辅助... 透视app"菠萝辅助器免费版的特点"详细辅助透牌教程(原先是有挂)这是由厦门游乐互动科技有限公司精心...
透视总结"aapok... 此外,数据分析德州(aapoker安装包怎么使用)辅助神器app还具备辅助透视行为开挂功能,通过对客...
透视私人局"哈糖大菠... 透视私人局"哈糖大菠萝破解器"详细辅助微扑克教程(好像是有挂);玩家必备必赢加哟《136704302...
透视中牌率"wepo... 透视中牌率"wepoker透视底牌"详细辅助介绍教程(一向真的有挂);小薇(透视辅助)致您一封信;亲...
透视游戏"拱趴大菠萝... 《透视游戏"拱趴大菠萝怎么开挂"详细辅助AI教程(本然是真的有挂)》 拱趴大菠萝怎么开挂软件透明挂更...
透视教程"hhpok... 此外,数据分析德州(hhpoker是正品吗)辅助神器app还具备辅助透视行为开挂功能,通过对客户hh...
透视攻略"wpk透视... 《透视攻略"wpk透视是真的假的"详细辅助详细教程(切实真的是有挂)》 wpk透视是真的假的软件透明...
透视教学"wepok... 透视教学"wepoker买脚本靠谱吗"详细辅助解密教程(最初真的是有挂);1.wepoker买脚本靠...
透视脚本"来玩app... 透视脚本"来玩app 德州 辅助"详细辅助2025版教程(一向有挂);建议优先通过来玩app 德州 ...