是的,Angular中可以使用一种类似于NodeJs Express风格的参数方式来实现路由。这种方式被称为矩阵参数。要使用矩阵参数,需要在路由配置中指定路由参数的格式。以下是一个示例:
const routes: Routes = [
{
path: 'products(/:category)(/:subCategory)',
component: ProductListComponent
}
];
在上面的示例中,路由路径中的冒号表示参数。这些参数可以在组件中使用ActivatedRoute服务的snapshot属性中获取。以下是一个示例:
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
template: `
Products in {{ category }} > {{ subCategory }}
`
})
export class ProductListComponent implements OnInit {
category: string;
subCategory: string;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.category = this.route.snapshot.paramMap.get('category');
this.subCategory = this.route.snapshot.paramMap.get('subCategory');
}
}
在上面的示例中,我们获取了路由参数,然后将它们显示在模板中。我们还可以使用route.params属性来订阅参数变化。