Angular2 different route parameters

前端 未结 1 801
孤独总比滥情好
孤独总比滥情好 2021-01-16 08:12

I want to have multiple functions in my appication activated through different parameters. The problem is that I have to distinguish between the routes. At the moment I need

相关标签:
1条回答
  • 2021-01-16 08:31

    You cannot map two path with the same exact segments as parameter. However, you can manually dispatch to the correct component. You can for exemple use this code.

    Routes

    export const EmployeeManagementRoutes: RouterConfig = [
    {
    path: 'employee-management',
    component: EmployeeManagementComponent,
    children: [
      { 
        path: '',  
        component: EmployeeManagementTableComponent,
        pathMatch: 'full'
      },
      { 
        path: ':type/:param',  
        component: EmployeeManagementTableComponent 
      }
    ]
    }
    ];
    

    Parameter dispatch

    this.route.params.subscribe(
            params => {  
                this.logger.log("Param "+params);
                    let type = params['type'];
                    this.logger.log("Type "+type);
                    let p = params['params'];
                    if(type === "employee"){
                        if(p.match(/\d+/)){
                            let id = +p;
                            this.logger.log("ID "+id);
                            this.editEmployee(id);
                        } else {
                            let option = p;
                            this.logger.log("Option "+option);
                            this.doEmployeeOption(option);  
                        }
                    }else if(type === "department"){
                        if(p){
                            let option = p;
                            this.logger.log("Option "+option);
                            this.doDepartmentOption(option);
                        }
                    }
            },
            //Error
            err => this.logger.error(err),
            //Complete
            () => {}
        );
    
    0 讨论(0)
提交回复
热议问题