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

相关内容

热门资讯

透视数据!wepoker怎么设... 透视数据!wepoker怎么设置透视(透视)都是真的是有挂(详细辅助辅助教程)1、金币登录送、破产送...
记者爆料!hhpoker有透视... 1、记者爆料!hhpoker有透视功能吗,hhpoker德州透视,安装教程(有挂神器)2、进入游戏-...
透视代打!红龙poker辅助平... 透视代打!红龙poker辅助平台(透视)竟然存在有挂(详细辅助AI教程);1、玩家可以在红龙poke...
必备攻略!wepoker作弊视... 1、必备攻略!wepoker作弊视频,wepoker插件功能辅助器,细节揭秘(有挂方法)2、进入游戏...
透视规律!wepoker怎么看... 透视规律!wepoker怎么看底牌(透视)切实有挂(详细辅助普及教程);进入游戏-大厅左侧-新手福利...
盘点一款!wepoker脚本,... 1、盘点一款!wepoker脚本,哈糖大菠萝挂,详细教程(有挂教程)。2、哈糖大菠萝挂透视辅助简单,...
透视代打!菠萝辅助器免费版的特... 透视代打!菠萝辅助器免费版的特点(透视)一贯真的有挂(详细辅助2025新版教程);1、玩家可以在软件...
重大消息!wepoker辅助器... 重大消息!wepoker辅助器最新版本更新内容,pokernow辅助工具,可靠教程(有挂辅助);支持...
透视总结!pokernow辅助... 透视总结!pokernow辅助工具(透视)好像存在有挂(详细辅助可靠技巧);一、软件透明挂的定义与意...
玩家必知教程!佛手在线大菠萝智... 玩家必知教程!佛手在线大菠萝智能辅助器,wepoker免费钻石,可靠技巧(有挂技巧)是一款可以让一直...