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

相关内容

热门资讯

第三分钟辅助!wejoker私... 第三分钟辅助!wejoker私人辅助软件,hhpkoer辅助器,策略教程(今日头条)1、进入游戏-大...
八分钟辅助!aapoker辅助... 八分钟辅助!aapoker辅助器是真的吗,wpk俱乐部有没有辅助,项目教程(真实有挂)1、打开软件启...
1分钟辅助!智星菠萝透视,哈糖... 1分钟辅助!智星菠萝透视,哈糖大菠萝软件下载,技法教程(详细教程)该软件可以轻松地帮助玩家将哈糖大菠...
三分钟辅助!wpk真吗,pok... 三分钟辅助!wpk真吗,pokermaster辅助器,操作教程(有挂技巧);1、三分钟辅助!wpk真...
1分钟辅助!wepoker游戏... 1分钟辅助!wepoker游戏的安装教程,We poker辅助器下载,操作教程(有挂存在)1、wep...
第八分钟辅助!wepoker透... 第八分钟辅助!wepoker透视底牌脚本,德普之星怎么设置埋牌,手段教程(有挂教学)1、全新机制【德...
第3分钟辅助!wepoker透... 第3分钟辅助!wepoker透视最简单三个步骤,wepokerplus作弊,绝活教程(有挂详情)运w...
九分钟辅助!steampoke... 九分钟辅助!steampokermaster辅助,wpk作弊是真的吗,妙计教程(有挂教程)1、ste...
7分钟辅助!wepokerpl... 7分钟辅助!wepokerplus外挂,wepoker开辅助能查到吗,方式教程(的确有挂)1、全新机...
第九分钟辅助!wepoker辅... 第九分钟辅助!wepoker辅助软件视频,wpk透视是真的吗,窍要教程(有挂透明挂)1、游戏颠覆性的...