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

相关内容

热门资讯

玩家必看科普!德普之星辅助挂多... 玩家必看科普!德普之星辅助挂多功能透视工具,一贯真的是有挂(有挂规律)1、点击下载安装,德普之星脚本...
最新技巧!wepoker辅助挂... 最新技巧!wepoker辅助挂多功能透视工具,竟然存在有挂(有挂技巧)1、完成wepoker有辅助插...
三分钟了解!aapoker辅助... 三分钟了解!aapoker辅助挂多功能透视工具,都是真的有挂(有挂功能)aapoker透视方法中分为...
一分钟揭秘!智星菠萝德州辅助挂... 一分钟揭秘!智星菠萝德州辅助挂多功能透视工具,切实真的有挂(有挂助手)一分钟揭秘!智星菠萝德州辅助挂...
重要通知!智星菠萝德州辅助挂多... 重要通知!智星菠萝德州辅助挂多功能透视工具,切实真的是有挂(真的有挂)1、不需要AI权限,帮助你快速...
每日必看教程!智星菠萝德州辅助... 每日必看教程!智星菠萝德州辅助挂多功能透视工具,确实真的是有挂(详细教程)1、智星菠萝德州免费脚本咨...
重磅来袭!德普之星辅助挂多功能... 重磅来袭!德普之星辅助挂多功能透视工具,都是是真的有挂(有挂透明挂)一、德普之星可以开透视的定义与意...
让我来分享经验!德扑圈辅助挂多... 让我来分享经验!德扑圈辅助挂多功能透视工具,本来真的有挂(有挂猫腻)1、上手简单,内置详细流程视频教...
重大通报!智星菠萝德州辅助挂多... 重大通报!智星菠萝德州辅助挂多功能透视工具,本来是有挂(有挂实锤)1、智星菠萝德州免费脚本咨询教程、...
玩家必看科普!微扑克辅助挂多功... 玩家必看科普!微扑克辅助挂多功能透视工具,原来是真的有挂(果真有挂)1、点击下载安装,微扑克脚本分类...