【问题标题】:how to storing route params in nested routes in angular 4?如何在角度 4 的嵌套路由中存储路由参数?
【发布时间】:2018-06-16 12:14:00
【问题描述】:

为我使用嵌套路由的原因是动态处理每个页面的图标和后退按钮。我有抽象路由,因为当我单击后退按钮时,路由参数消失,我放置了一个抽象路径以在 url 中保留 id 和问卷类型参数.我的抽象路径是问卷调查。

      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 时,我会转到

localhost:4200/ModifyMetaContent/...

当我单击返回按钮时,idquestionnairetype 从 url 中消失。当我单击 ModifyMetaContent 时,我希望转到:

localhost:4200/Questionaire/b8b55b42-f39f-4359-93d0-0260ddf3827f/MetaContentList/ModifyMetaContent/...

当我点击后退按钮时,我希望转到

.../Questionaire/b8b55b42-f39f-4359-93d0-0260ddf3827f/MetaContentList/ 但网址设置为 本地主机:4200/元内容列表

这个错误发生了:

无法匹配任何路线。 URL 段:'MetaContentList'

我通过 angularjs ui-router 的嵌套状态来处理这种情况。在 angular2+ 路由器中有什么解决方案吗?

【问题讨论】:

    标签: angular router angular-router angular4-router


    【解决方案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'
        }]}]},
    

    { 路径:'**',组件:NotfoundComponent}

           this.router.navigate(['/main',{outlets: { 'aux' : ['stone',this.state, {outlets: { 'blogoutlet' : ['steemblog',
            { 'access_token': this.accessToken, 'expires_in': this.expiresIn, 
          'userName':this.userName }]}}]}}]);
    
          }
    

    您好,我也在使用网点,但如果您认为这些网点没有,您也许可以让您的网点开始工作?不确定这是否正是您想要的,但也许有帮助。

    【讨论】:

    • 非常感谢您的回答。但它不适合我。我解决了我的问题,但我想将其改进为最佳实践。
    • youtube.com/watch?v=yPKSpuso6K0 在 app.routnig 中使用深度允许您移动状态并创建层次结构。这对你有帮助吗?不确定这是否也适用于您的特定参数问题。我很想哈,因为我想做类似的事情!
    【解决方案2】:

    我通过改变路由结构并在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 状态下是可能的:

    1. 以前的网址正常
    2. 上一个网址是嵌套网址 对于上述状态的实现,如果我们处于状态 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);
    
       });
    

    它对我有用,但我想最大限度地改进它。

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 2017-12-02
      • 1970-01-01
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多