如何在 angular 4 的嵌套路由中存储路由参数?
how to storing route params in nested routes in angular 4?
我使用嵌套路由的原因是动态处理每个 page.i 具有抽象路由的图标和后退按钮,因为当我单击后退按钮时,路由参数消失并且我放置了一个抽象路径来保留 ID 和问卷url.my 抽象路径中的类型参数是问卷。
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children:[
{ path: 'Home', component: HomeComponent,data:{icon:'fa-home'} },
{ path: 'QuestionaireList', component: QuestionaireListComponent,data:{icon:'fa-list-ul',previousState:'/Home'} },
{ path: 'ModifyMetaContent/:metaContentId/:title', component: ModifyMetaContentComponent,data:{icon:'fa-database',previousState:'/MetaContentList'}}
{
path: 'Questionaire/:id',
children:[
{ path: 'MetaContentList', component: MetaContentListComponent,data:{icon:'fa-database',previousState:'/QuestionaireList'}}
]},
{
path: 'Questionaire/:questionnaireType',
children:
[
{ path: 'AddQuestionnaire', component: CreateQuestionnaireComponent,data:{icon:'fa-plus',previousState:'/QuestionaireList'}},
]},
{
path: 'Questionaire/:questionnaireType/:id',
children:[
{ path: 'UpdateQuestionnaire', component: EditComponent,data:{icon:'fa-pencil',previousState:'/QuestionaireList'} },
{ path: 'QuestionList', component: QuestionListComponent,data:{icon:'fa-pencil',previousState:'/QuestionaireList'} },
{ path: 'ImportQuestion', component: ImportQuestionComponent,data:{icon:'fa-plus',previousState:'/QuestionaireList'} },
]} ,
]},
];
MetaContentList.Component.html
<a class="ui labeled positive icon small button" [routerLink]="['/ModifyMetaContent','',questionnaireTitle]">
<i class="plus icon"></i>
new metaContent
</a>
我将之前的 url 存储在 snapshot.data.previousState
中,但是当我单击 ModifyMetaContent
时,我会转到
localhost:4200/ModifyMetaContent/...
当我点击后退按钮时,id
和 questionnairetype
从 url.when 中消失,我点击 ModifyMetaContent 我预计会转到:
localhost:4200/Questionaire/b8b55b42-f39f-4359-93d0-0260ddf3827f/MetaContentList/ModifyMetaContent/...
当我点击后退按钮时,我预计会转到
.../Questionaire/b8b55b42-f39f-4359-93d0-0260ddf3827f/MetaContentList/
but url set to
localhost:4200/MetaContentList
并且发生了这个错误:
Cannot match any routes. URL Segment: 'MetaContentList'
我通过 angularjs 的嵌套状态来处理这种情况 ui-router.is angular2+ 路由器中这个问题的任何解决方案?
{ path: '', redirectTo: 'main', pathMatch: 'full'
},
{ path: 'header', component: HeaderComponent},
{ path: 'login', component: LoginComponent},
{ path: 'logout', component: LogoutComponent},
{ path: 'register', component: RegisterComponent},
{ path: 'profile', children: [
{ path: ':id', component: ProfileComponent,children: [
{ path: 'profileform/:id', component: ProfileformComponent,outlet: 'formOutlet', pathMatch: 'full' }]}]},
{ path: 'main', component: MainComponent, children: [
{ path: 'stone/:id', component: StoneComponent,outlet: 'aux', children: [
{ path: 'steemblog', component: BlogComponent,outlet: 'blogoutlet'},
{path: 'extlogin', component: ExternalLoginComponent, outlet:'blogoutlet'
}]}]},
{ 路径:'**',组件:NotfoundComponent}
this.router.navigate(['/main',{outlets: { 'aux' : ['stone',this.state, {outlets: { 'blogoutlet' : ['steemblog',
{ 'access_token': this.accessToken, 'expires_in': this.expiresIn,
'userName':this.userName }]}}]}}]);
}
您好,我也在使用插座,但是如果您认为那些不在您身边,您也许可以让您的插座工作?不确定这是否正是您想要的,但也许有帮助。
我通过更改路由结构并在 appcomponent 中为 previousstate link 添加一个函数来解决这个问题:
我的路由器:
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children:[
{ path: 'Home', component: HomeComponent,data:{icon:'fa-home',previousState:[]} },
{ path: 'QuestionaireList', component: QuestionaireListComponent,data:{icon:'fa-list-ul',previousState:['/Home']} },
{ path: 'Questionaire/:id',children:[
{ path: 'MetaContentList', component: MetaContentListComponent,data:{icon:'fa-database',previousState:['/QuestionaireList']}},
{ path: 'ModifyMetaContent/:metaContentId/:title', component: ModifyMetaContentComponent,data:{icon:'fa-database',previousState:['Questionaire','MetaContentList']}}
]},
{ path: 'Questionaire/:questionnaireType',
children:[{ path: 'AddQuestionnaire', component: CreateQuestionnaireComponent,data:{icon:'fa-plus',previousState:['/QuestionaireList']}}]},
{path: 'Questionaire/:questionnaireType/:id',
children:[
{ path: 'UpdateQuestionnaire', component: EditComponent,data:{icon:'fa-pencil',previousState:['/QuestionaireList']} },
{ path: 'QuestionList', component: QuestionListComponent,data:{icon:'fa-pencil',previousState:['/QuestionaireList']} },
{ path: 'ImportQuestion', component: ImportQuestionComponent,data:{icon:'fa-upload',previousState:['/QuestionaireList']} },
]} ,
]},
];
在我的路由器 2 状态下是可能的:
- 上一个url正常
- 上一个 url 是嵌套的 url
对于上述状态的实现,如果我们处于状态 2
array.length
大于 1,我将 previousstate 放入数组中。为了嵌入路由参数,我在 appcomponent: 中创建了一个方法
获取 lastchild 的方法:
getSnapshot(route: ActivatedRoute){
let lastChild = route;
while (lastChild.firstChild) {
lastChild = lastChild.firstChild;
}
return lastChild;
}
获取先前状态的方法:
getPreviousUrl(a):string{
let previousState:string;
const previousStateLength=a.snapshot.data.previousState ? a.snapshot.data.previousState.length :null;
if(previousStateLength!==null){
if(previousStateLength<=1 )
{previousState=a.snapshot.data.previousState[0];}
else{
previousState='/';
let parentParams=[''];
let j:number=0;
let b=a;
while(b.parent){
b.parent.params.subscribe((params)=>{
Object.keys(params).map(key=>parentParams[j]+=params[key]+'/');
for(let i=0;i<previousStateLength-1;i++){
if(parentParams[j]!==undefined)
previousState+=a.snapshot.data.previousState[i]+'/'+parentParams[j];
}
})
b=b.parent;
j++;
}
previousState+=a.snapshot.data.previousState[previousStateLength-1];
}
return previousState;
}
}
并获取数据:
constructor(router:Router,route:ActivatedRoute) {
router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(e => {
const lastChild=this.getSnapshot(route);
this.routerIcon = lastChild.snapshot.data.icon;
this.previousState=this.getPreviousUrl(lastChild);
});
它对我有用,但我想最大限度地改进它。
我使用嵌套路由的原因是动态处理每个 page.i 具有抽象路由的图标和后退按钮,因为当我单击后退按钮时,路由参数消失并且我放置了一个抽象路径来保留 ID 和问卷url.my 抽象路径中的类型参数是问卷。
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children:[
{ path: 'Home', component: HomeComponent,data:{icon:'fa-home'} },
{ path: 'QuestionaireList', component: QuestionaireListComponent,data:{icon:'fa-list-ul',previousState:'/Home'} },
{ path: 'ModifyMetaContent/:metaContentId/:title', component: ModifyMetaContentComponent,data:{icon:'fa-database',previousState:'/MetaContentList'}}
{
path: 'Questionaire/:id',
children:[
{ path: 'MetaContentList', component: MetaContentListComponent,data:{icon:'fa-database',previousState:'/QuestionaireList'}}
]},
{
path: 'Questionaire/:questionnaireType',
children:
[
{ path: 'AddQuestionnaire', component: CreateQuestionnaireComponent,data:{icon:'fa-plus',previousState:'/QuestionaireList'}},
]},
{
path: 'Questionaire/:questionnaireType/:id',
children:[
{ path: 'UpdateQuestionnaire', component: EditComponent,data:{icon:'fa-pencil',previousState:'/QuestionaireList'} },
{ path: 'QuestionList', component: QuestionListComponent,data:{icon:'fa-pencil',previousState:'/QuestionaireList'} },
{ path: 'ImportQuestion', component: ImportQuestionComponent,data:{icon:'fa-plus',previousState:'/QuestionaireList'} },
]} ,
]},
];
MetaContentList.Component.html
<a class="ui labeled positive icon small button" [routerLink]="['/ModifyMetaContent','',questionnaireTitle]">
<i class="plus icon"></i>
new metaContent
</a>
我将之前的 url 存储在 snapshot.data.previousState
中,但是当我单击 ModifyMetaContent
时,我会转到
localhost:4200/ModifyMetaContent/...
当我点击后退按钮时,id
和 questionnairetype
从 url.when 中消失,我点击 ModifyMetaContent 我预计会转到:
localhost:4200/Questionaire/b8b55b42-f39f-4359-93d0-0260ddf3827f/MetaContentList/ModifyMetaContent/...
当我点击后退按钮时,我预计会转到
.../Questionaire/b8b55b42-f39f-4359-93d0-0260ddf3827f/MetaContentList/ but url set to localhost:4200/MetaContentList
并且发生了这个错误:
Cannot match any routes. URL Segment: 'MetaContentList'
我通过 angularjs 的嵌套状态来处理这种情况 ui-router.is angular2+ 路由器中这个问题的任何解决方案?
{ path: '', redirectTo: 'main', pathMatch: 'full'
},
{ path: 'header', component: HeaderComponent},
{ path: 'login', component: LoginComponent},
{ path: 'logout', component: LogoutComponent},
{ path: 'register', component: RegisterComponent},
{ path: 'profile', children: [
{ path: ':id', component: ProfileComponent,children: [
{ path: 'profileform/:id', component: ProfileformComponent,outlet: 'formOutlet', pathMatch: 'full' }]}]},
{ path: 'main', component: MainComponent, children: [
{ path: 'stone/:id', component: StoneComponent,outlet: 'aux', children: [
{ path: 'steemblog', component: BlogComponent,outlet: 'blogoutlet'},
{path: 'extlogin', component: ExternalLoginComponent, outlet:'blogoutlet'
}]}]},
{ 路径:'**',组件:NotfoundComponent}
this.router.navigate(['/main',{outlets: { 'aux' : ['stone',this.state, {outlets: { 'blogoutlet' : ['steemblog',
{ 'access_token': this.accessToken, 'expires_in': this.expiresIn,
'userName':this.userName }]}}]}}]);
}
您好,我也在使用插座,但是如果您认为那些不在您身边,您也许可以让您的插座工作?不确定这是否正是您想要的,但也许有帮助。
我通过更改路由结构并在 appcomponent 中为 previousstate link 添加一个函数来解决这个问题:
我的路由器:
const routes: Routes = [
{
path: '',
component: LayoutComponent,
children:[
{ path: 'Home', component: HomeComponent,data:{icon:'fa-home',previousState:[]} },
{ path: 'QuestionaireList', component: QuestionaireListComponent,data:{icon:'fa-list-ul',previousState:['/Home']} },
{ path: 'Questionaire/:id',children:[
{ path: 'MetaContentList', component: MetaContentListComponent,data:{icon:'fa-database',previousState:['/QuestionaireList']}},
{ path: 'ModifyMetaContent/:metaContentId/:title', component: ModifyMetaContentComponent,data:{icon:'fa-database',previousState:['Questionaire','MetaContentList']}}
]},
{ path: 'Questionaire/:questionnaireType',
children:[{ path: 'AddQuestionnaire', component: CreateQuestionnaireComponent,data:{icon:'fa-plus',previousState:['/QuestionaireList']}}]},
{path: 'Questionaire/:questionnaireType/:id',
children:[
{ path: 'UpdateQuestionnaire', component: EditComponent,data:{icon:'fa-pencil',previousState:['/QuestionaireList']} },
{ path: 'QuestionList', component: QuestionListComponent,data:{icon:'fa-pencil',previousState:['/QuestionaireList']} },
{ path: 'ImportQuestion', component: ImportQuestionComponent,data:{icon:'fa-upload',previousState:['/QuestionaireList']} },
]} ,
]},
];
在我的路由器 2 状态下是可能的:
- 上一个url正常
- 上一个 url 是嵌套的 url
对于上述状态的实现,如果我们处于状态 2
array.length
大于 1,我将 previousstate 放入数组中。为了嵌入路由参数,我在 appcomponent: 中创建了一个方法
获取 lastchild 的方法:
getSnapshot(route: ActivatedRoute){
let lastChild = route;
while (lastChild.firstChild) {
lastChild = lastChild.firstChild;
}
return lastChild;
}
获取先前状态的方法:
getPreviousUrl(a):string{
let previousState:string;
const previousStateLength=a.snapshot.data.previousState ? a.snapshot.data.previousState.length :null;
if(previousStateLength!==null){
if(previousStateLength<=1 )
{previousState=a.snapshot.data.previousState[0];}
else{
previousState='/';
let parentParams=[''];
let j:number=0;
let b=a;
while(b.parent){
b.parent.params.subscribe((params)=>{
Object.keys(params).map(key=>parentParams[j]+=params[key]+'/');
for(let i=0;i<previousStateLength-1;i++){
if(parentParams[j]!==undefined)
previousState+=a.snapshot.data.previousState[i]+'/'+parentParams[j];
}
})
b=b.parent;
j++;
}
previousState+=a.snapshot.data.previousState[previousStateLength-1];
}
return previousState;
}
}
并获取数据:
constructor(router:Router,route:ActivatedRoute) {
router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(e => {
const lastChild=this.getSnapshot(route);
this.routerIcon = lastChild.snapshot.data.icon;
this.previousState=this.getPreviousUrl(lastChild);
});
它对我有用,但我想最大限度地改进它。