是的,Angular子路由可以以参数开始。你可以在父路由中定义参数,并在子路由中使用这些参数。
下面是一个示例:
在父路由中定义参数:
const routes: Routes = [
{
path: 'parent/:id',
component: ParentComponent,
children: [
{
path: '',
redirectTo: 'child',
pathMatch: 'full'
},
{
path: 'child',
component: ChildComponent
}
]
}
];
在子路由中使用参数:
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-child',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
id: string;
constructor(private route: ActivatedRoute) { }
ngOnInit(): void {
this.route.paramMap.subscribe(params => {
this.id = params.get('id');
});
}
}
在上面的例子中,父路由的路径为parent/:id
,其中:id
是一个参数。在子路由中,我们使用ActivatedRoute
服务来访问父路由中的参数。在ngOnInit
生命周期钩子中,我们订阅了paramMap
来获取参数的值,并将其赋给id
属性。
希望这个示例能够帮助到你!