Javascript 角度:在路由内传递可选数据
我使用的是angular 5,我有一个长路由文件:Javascript 角度:在路由内传递可选数据,javascript,angular,typescript,angular5,angular-routing,Javascript,Angular,Typescript,Angular5,Angular Routing,我使用的是angular 5,我有一个长路由文件: const homeRoutes: Routes = [ { path: 'home', component: HomeComponent, children: [ { path: 'registration', component: RegistrationComponent, children: [ { path: '
const homeRoutes: Routes = [
{
path: 'home', component: HomeComponent,
children: [
{
path: 'registration',
component: RegistrationComponent,
children: [
{
path: 'synthese',
component: SyntheseComponent
},
{
path: 'queue',
component: QueueComponent,
children: [
{
path: 'queue-modal',
component: QueueModalComponent
},
{
path: 'confirm',
component: ConfirmComponent
}
]
}
]
},
{
...
我想在“注册”路径中传递数据
正如我被告知的,我需要这样写:path:'registration/:mydata'
然后订阅ActivatedRoute的数据
问题是我并不总是传递数据,只是在某些情况下
如何使影响最小化???您可以使用查询字符串参数(AKA)而不是路由参数。您不需要在路由中定义查询参数 以下是相对href的示例:
registration/?mydata=123
这就是如何为路由链接定义查询参数的方法:
转到注册
以下是读取参数值的方法:
myValue:any;
构造函数(专用路由:ActivatedRoute){}
恩戈尼尼特(){
this.route.queryParams.subscribe(参数=>{
this.myValue=params['mydata'];
});
}
也许你可以传递一个空值,当你使用Activatetour检查它是否为空?你可以创建两个路由注册/:mydata
和注册
类似于文档中的示例: