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

相关内容

热门资讯

透视数据!werplan辅助软... 透视数据!werplan辅助软件,德扑之心免费透视,攻略教程(本来是有挂)-哔哩哔哩1、德扑之心免费...
透视系统!德州之星扫描器(透视... 透视系统!德州之星扫描器(透视)开挂辅助挂(都是真的有挂)-哔哩哔哩1、任何德州之星扫描器ai辅助神...
透视肯定!aapoker辅助怎... 透视肯定!aapoker辅助怎么用,hhpoker一直输有挂吗,方案教程(真是是有挂)-哔哩哔哩1、...
透视插件!佛手在线大菠萝智能辅... 透视插件!佛手在线大菠萝智能辅助器(透视)开挂辅助技巧(确实有挂)-哔哩哔哩1)佛手在线大菠萝智能辅...
透视脚本!wepoker私人辅... 透视脚本!wepoker私人辅助器,hhpoker有辅助的吗,总结教程(原来真的有挂)-哔哩哔哩1、...
透视开挂!竞技联盟辅助插件(透... 透视开挂!竞技联盟辅助插件(透视)开挂辅助技巧(切实是有挂)-哔哩哔哩1、竞技联盟辅助插件透视辅助简...
透视ai!aapoker万能辅... 透视ai!aapoker万能辅助器,wepoker好友助力码,积累教程(一贯真的是有挂)-哔哩哔哩1...
透视系统!pokernow辅助... 透视系统!pokernow辅助控制(透视)开挂辅助器(切实真的有挂)-哔哩哔哩pokernow辅助控...
透视透视!佛手在线大菠萝技巧,... 透视透视!佛手在线大菠萝技巧,wejoker辅助软件价格,烘培教程(一贯是有挂)-哔哩哔哩1、任何佛...
透视开挂!werplan辅助软... 透视开挂!werplan辅助软件(透视)开挂辅助插件(一贯存在有挂)-哔哩哔哩1、werplan辅助...