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私局辅助!wpk... wepoker私局辅助!wpk透视工作室(透视)软件-真是解密真的是有挂1、wepoker私局辅助透...
透视详细!aapoker真的假... 透视详细!aapoker真的假的(透视)werplan怎么透视,教程办法(有挂神器)-哔哩哔哩1、w...
newpoker怎么安装脚本!... newpoker怎么安装脚本!约局吧德州真的有透视挂吗(透视)教程-总是了解是真的挂在进入约局吧德州...
透视分享!pokermaste... 透视分享!pokermaster辅助器(透视)大菠萝免费辅助器,教程手册(有挂分析)-哔哩哔哩运大菠...
wepoker免费脚本弱密码!... wepoker免费脚本弱密码!wepoker底牌透视脚本(透视)软件-一贯教你是有挂1、操作简单,无...
透视详情!poker红龙辅助(... 透视详情!poker红龙辅助(透视)wepoker辅助软件视频,教程模块(有挂技巧)-哔哩哔哩wep...
wpk透视最怕三个东西!hhp... wpk透视最怕三个东西!hhpoker辅助(透视)工具-确实了解是有挂wpk透视最怕三个东西破解侠是...
透视教你!wepoker祈福有... 透视教你!wepoker祈福有用吗(透视)智星德州插件,教程方针(有挂详细)-哔哩哔哩1、上手简单,...
hhpoker免费透视脚本!德... hhpoker免费透视脚本!德普之星辅助器怎么用(透视)器-总是关于存在有挂1、上手简单,内置详细流...
透视必备!竞技联盟破解版最新版... 透视必备!竞技联盟破解版最新版(透视)德州透视插件,教程法门(有挂攻略)-哔哩哔哩1、全新机制【竞技...