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、这...
揭秘几款!腾微填大坑辅助,天天... 揭秘几款!腾微填大坑辅助,天天贵阳辅助工具,详细挂(有挂方略)所有人都在同一条线上,像星星一样排成一...
今年以来!德州圈脚本,智星菠萝... 今年以来!德州圈脚本,智星菠萝可以辅助吗,原来真的有挂(今日头条)1、智星菠萝可以辅助吗辅助器安装包...
总算了解!悟空大厅辅助器,广东... 总算了解!悟空大厅辅助器,广东闲来辅助神器,详细工具(有挂秘诀)1、这是跨平台的广东闲来辅助神器轻量...
有玩家发现!aapoker怎么... 有玩家发现!aapoker怎么拿好牌,德普之星透视,其实真的是有挂(果真有挂)1、德普之星透视模拟器...
玩家必看秘籍!聚友联盟辅助器,... 玩家必看秘籍!聚友联盟辅助器,雀神麻雀充运势用吗,详细脚本(新版有挂)雀神麻雀充运势用吗脚本下载中分...
截至目前!wejoker辅助脚... 截至目前!wejoker辅助脚本,德普之星透视辅助软件,切实确实有挂(真实有挂)一、德普之星透视辅助...
程序员教你!欢乐卡五星插件,微... 程序员教你!欢乐卡五星插件,微信新海贝之城辅助,详细方法(竟然有挂)1、点击下载安装,微信新海贝之城...
有了最新消息!we-poker... 有了最新消息!we-poker正规吗,德扑之星辅助器,果然确实有挂(有挂猫腻)1、完成德扑之星辅助器...
科技通报!小闲川南辅助工具,福... 科技通报!小闲川南辅助工具,福建大玩家辅助是真的吗,详细软件(有挂助手)1、福建大玩家辅助是真的吗辅...