为我使用嵌套路由的原因是为每个页面处理动态图标和后退按钮。我有抽象路线,因为当我点击后退按钮路线参数消失时,我把一个抽象路径保存在网址中的id和问卷类型参数.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>
我将之前的网址存储在snapshot.data.previousState
,但是当我点击ModifyMetaContent
时,我会转到
本地主机:4200 / ModifyMetaContent /...
当我点击后退按钮时,id
和questionnairetype
从url中消失。当我点击ModifyMetaContent时,我预计会转到:
本地主机:4200 /问卷/ b8b55b42-f39f-4359-93d0-0260ddf3827f / MetaContentList / ModifyMetaContent /...
当我点击后退按钮我希望转到
... /问卷/ b8b55b42-f39f-4359-93d0-0260ddf3827f / MetaContentList / 但网址设置为 本地主机:4200 / MetaContentList
发生了这个错误:
无法匹配任何路线。网址细分:&#39; MetaContentList&#39;
我通过angularjs uirouter的嵌套状态来处理这种情况。在angular2 +路由器中这个问题的解决方案是什么?
答案 0 :(得分:1)
{ 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'
}]}]},
{path:'**',component:NotfoundComponent}
this.router.navigate(['/main',{outlets: { 'aux' : ['stone',this.state, {outlets: { 'blogoutlet' : ['steemblog',
{ 'access_token': this.accessToken, 'expires_in': this.expiresIn,
'userName':this.userName }]}}]}}]);
}
您好我也在使用网点,但如果您认为那些人可能会让您的工作?不确定这是否正是你想要的,但也许它会有所帮助。
答案 1 :(得分:1)
我通过更改路径结构解决了这个问题,并为appcomponent中的previousstate链接添加了一个函数:
我的路由器:
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状态下是可能的:
array.length
大于1,则将previousstate放在数组中。对于嵌入路由参数,我在appcomponent中创建一个方法:get 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);
});
它的工作对我来说,我想要极大地改善它。