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底牌透视,wepoker网页版透视方法,存在挂教程(有挂教程)关于wep...
发现玩家!德普之星辅助器,we... 1、发现玩家!德普之星辅助器,wepoker手机插件,曝光教程(有挂技巧);详细教程。2、德普之星辅...
一起来讨论!德州局透视脚本免费... 一起来讨论!德州局透视脚本免费版下载手机版,wepoker私人局俱乐部,AA德州教程(有挂技巧);致...
技术分享!we poker辅助... 技术分享!we poker辅助器v3.3,aapoker ai插件,详细教程(有挂攻略);亲真的是有...
实测必看!wepoker智能辅... 实测必看!wepoker智能辅助插件,wepoker轻量版透视系统,我来教教你(有挂神器);原来确实...
玩家必看!wepoker私人局... 玩家必看!wepoker私人局外卦,wepoker怎么设置透视,可靠技巧(有挂攻略)是一款可以让一直...
实测发现!wepoker免费透... 实测发现!wepoker免费透视,pokemomo辅助软件,攻略教程(有挂软件)1、点击下载安装,微...
让我来分享经验!wepoker... 让我来分享经验!wepoker智能辅助插件,wepoker辅助透视,曝光教程(有挂方法);《WPK辅...
科普!aapoker插件,we... 科普!aapoker插件,wepoker辅助软件视频,安装教程(有挂透明);支持多人共享记分板与复盘...
必知教程!wepoker好友助... 必知教程!wepoker好友助力码,aapoker透视怎么用,AA德州教程(有挂技巧)准备好在aap...