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

相关内容

热门资讯

wpk透视辅助!微扑克机器人代... wpk透视辅助!微扑克机器人代打,(德州app)从前真的有挂(详细透视揭秘教程);实战中需综合运用上...
透视ai代打(微扑克ai辅助)... 透视ai代打(微扑克ai辅助)hm3德州辅助怎么购买(详细辅助揭秘教程)都是是有挂1、许多玩家不知道...
wpk透视辅助!wepoke小... 这是一款非常优秀的wpk透视辅助 ia辅助检测软件,能够让你了解到wpk透视辅助中牌率当中全部隐藏参...
透视游戏(wepoke ai辅... 透视游戏(wepoke ai辅助)pokerrrr开挂(详细辅助力荐教程)最初真的是有挂1、每一步都...
德州之星辅助挂!wepoke ... 德州之星辅助挂!wepoke ai代打,(AAPOker)往昔有挂(详细辅助安装教程);玩家在德州之...
透视安卓版(aapoker挂)... 透视安卓版(aapoker挂)wepower软件靠谱吗(详细辅助德州教程)都是有挂是一款可以让一直输...
aapoker透视辅助!wpk... aapoker透视辅助!wpk的下风机制,(wEPoke)一贯有挂(详细辅助分享教程)1、这是跨平台...
透视软件(aapoker挂)扑... 透视软件(aapoker挂)扑克时间辅助(详细辅助教你攻略)一贯存在有挂;小薇(透视辅助)致您一封信...
德州之星插件!微扑克俱乐部设置... 德州之星插件!微扑克俱乐部设置,(德州aapoker俱乐部)竟然有挂(详细透视曝光教程);是一款可以...
透视辅助(智星德州菠萝开挂)红... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...