通常,我使用这种路由在app-routing.module.ts中加载组件
const routes: Routes = [
{
path: '', component: HomeComponent
},
{
path: 'product-list', component: ProductListComponent
},
{
path: 'product', component: ProductComponent
}
];但是,我想将路由建立在http调用的基础上,以确定要加载的组件。
例如,我希望能够拥有以下URL:
/nike
/nike-airmax
第一个URL应加载产品列表页面,第二个URL应加载产品页面
我有一个http调用来获取页面类型,它看起来像这样:
this.http.get(domain + 'api/ApiRoute?url=' + this.activatedRoute(返回一个int值,但它可以返回我需要的任何值)
问题1
我不知道如何从app-routing-module.ts调用服务(返回类型
问题2
我不知道如何使用服务中的"type“来确定路由
发布于 2020-02-05 22:07:52
使用Angular内置路由是不可能的,因为它的配置应该是静态的。您可以做的最好的事情是在path: ':productName'上创建您自己的SwitchComponent,在那里您将能够使用*ngIf呈现所需的组件之一。
ngOnInit(){
this.activatedRoute.params.pipe(
switchMap((params) => this.myService.getComponentType(params['productName']))
).subscribe(type => this.componentType = type);
}
.....
<product *ngIf="componentType == 'product'"></product>
<product-list *ngIf="componentType == 'product-list'"></product-list>https://stackoverflow.com/questions/60077419
复制相似问题