[英]Setup dynamic routing in angular 8
I am facing some issue while setting up the routing in the angular 8. I am doing it like:在 angular 8 中设置路由时,我遇到了一些问题。我正在这样做:
'company/:id/activity'
'company/:id/contacts'
did not get any params in activatedRoute:在activatedRoute中没有得到任何参数:
this.activateRoute.params
.subscribe(param => console.log(param) )
Is there any luck to fix that?有没有运气来解决这个问题?
Main Routing file:主路由文件:
{
path: 'company/:id',
children: [
{
path: '',
loadChildren: () => import('@app/features/company-view/company-view.module').then(m => m.CompanyViewModule)
}
]
}
Lazy loaded routing file:延迟加载的路由文件:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CompanyViewComponent } from '@app/features/company-view/company-view.component';
import { PendingChangesGuard } from '@app/shared/guards';
import { CompanyActivityComponent } from './company-activity/company-activity.component';
const routes: Routes = [
{
path: '',
component: CompanyViewComponent,
children: [
{
path: '',
redirectTo: 'view'
},
{
path: 'activity',
component: CompanyActivityComponent,
data: {
title: "Company Activity"
}
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class CompanyViewRoutingModule { }
if I use routing below then it works fine:如果我使用下面的路由,那么它工作正常:
const routes: Routes = [
{
path: '',
component: CompanyViewComponent,
children: [
{
path: ':id/activity',
component: CompanyActivityComponent,
data: {
title: "Company Activity"
}
},
{
path: '**',
redirectTo: 'activity'
}
]
}
];
then how do I set the default routing to :id/activity
?那么如何将默认路由设置为:id/activity
?
So.所以。 You want to pass variables for your component through the routing.您想通过路由为您的组件传递变量。 In your routes.ts is oke like this path: 'company/:id/:activity'
.在您的 routes.ts 中可以像这样path: 'company/:id/:activity'
。 Then you need to give it to your component.然后你需要把它交给你的组件。 I'm using this: id = this.route.snapshot.params['id']
or activity = this.route.snapshot.params['activity']
.我正在使用这个: id = this.route.snapshot.params['id']
或activity = this.route.snapshot.params['activity']
。 From here you can pass through for another component or whatever you want.从这里你可以通过另一个组件或任何你想要的。
Ex:前任:
<app-testcomponent
[id]="id"
[activity]="activity">
</app-testcomponent>
I'm not seeing a route for this: "view".我没有看到这条路线:“查看”。
{
path: '',
redirectTo: 'view'
}
I changed it to "activity" and changed it to your catch all route for that section of the tree.我将其更改为“活动”,并将其更改为树的该部分的所有路线。 I also refactored your routes a little bit, please look at both:我还稍微重构了您的路线,请同时查看:
Main Route主要路线
{
path: 'company',
children: [
{
path: '',
loadChildren: () => import('@app/features/company-view/company-view.module').then(m => m.CompanyViewModule)
}
]
}
Child Routes子路线
const routes: Routes = [
{
path: ':id',
component: CompanyViewComponent,
children: [
{
path: 'activity',
component: CompanyActivityComponent,
data: {
title: "Company Activity"
}
},
{
path: '**',
redirectTo: 'activity'
}
]
}
];
You are trying to get the parent parameter in the child component.您正在尝试在子组件中获取父参数。 Use the below:使用以下内容:
this.activateRoute.parent.params.subscribe(params => {
console.log(params["id"]);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.