Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度路由总是在组件之前显示url_Javascript_Angular_Authentication_Routes_Guard - Fatal编程技术网

Javascript 角度路由总是在组件之前显示url

Javascript 角度路由总是在组件之前显示url,javascript,angular,authentication,routes,guard,Javascript,Angular,Authentication,Routes,Guard,我不知道会发生什么,但在我的Angular 7应用程序中,当我通过浏览器输入一些url时,它总是跟随我输入的url。 例如: 键入=>http://localhost:4200/xxx 结果=>http://localhost:4200/xxx/dashboard 键入=>http://localhost:4200/ooo 结果=>http://localhost:4200/ooo/dashboard 在路由跟踪urlxxx或ooo中无法读取,导航开始url始终为“/”(附图片) 这是我的Aut

我不知道会发生什么,但在我的Angular 7应用程序中,当我通过浏览器输入一些url时,它总是跟随我输入的url。 例如:

键入=>http://localhost:4200/xxx

结果=>http://localhost:4200/xxx/dashboard

键入=>http://localhost:4200/ooo

结果=>http://localhost:4200/ooo/dashboard

在路由跟踪urlxxxooo中无法读取,导航开始url始终为“/”(附图片)

这是我的AuthGuard:

canActivate(route: ActivatedRouteSnapshot) {
    const currentUser = this.service.currentUserValue;
    if (currentUser) {
        let roles = route.data['allowedRoles'] as Array<string>;
        if(roles){
            roles.forEach(role=>{
                if(currentUser.URole == role){
                    return true;
                }else{
                    this.router.navigateByUrl('/forbidden');
                    return false;
                }
            })
        }
        return true;
    }else{
        this.router.navigateByUrl('/login');
        return false;
    }
}
我在routes中启用跟踪,但我输入到浏览器的每个xxx都会显示“/”这是日志:

本指南来自Angular官方网站,将重定向放在目标重定向路由之后,将重定向放在通配符路由之前,因此您可能应该尝试以下方法:

const routes: Routes = [
  {path:'dashboard',component:DashboardComponent,canActivate: [AuthGuard]},
  {path:'section',component:SectionComponent,canActivate: [AuthGuard]},
  {path:'subsection',component:SubsectionComponent,canActivate: [AuthGuard]},
  {path:'equipment',component:EquipmentComponent,canActivate: [AuthGuard]},
  {path:'downtime',component:DowntimeComponent,canActivate: [AuthGuard]},
  {path:'user',component:UserComponent,canActivate: [AuthGuard],data:{allowedRoles:['Admin']}},
  {path:'mainfailure',component:MainfailureComponent,canActivate: [AuthGuard]},
  {path:'failureaction',component:FailureactionComponent,canActivate: [AuthGuard]},
  {path:'failuremechanism',component:FailuremechanismComponent,canActivate: [AuthGuard]},
  {path:'failuremode',component:FailuremodeComponent,canActivate: [AuthGuard]},
  {path:'failurecause',component:FailurecauseComponent,canActivate: [AuthGuard]},
  {path:'login',component:LoginComponent},
  {path:'forbidden',component:ForbiddenComponent},
  {path:'',redirectTo:'dashboard',pathMatch:'full'},
  {path:'**',redirectTo:'dashboard'}
];

谢谢,在搜索这么长时间后,问题不在routemodule或auth中,我不小心将index.html中的base href从“/”更改为“

请显示一个重定向到仪表板的按钮或链接的示例。@PedroLima通过链接访问时可以,但当我键入浏览器url时,输入总是跟随,例如:http:/localhost:4200/dosa然后浏览器url结果是http:localhost:4200/dosa/home/dashboard谢谢,在搜索这么长时间后,问题不在routemodule或auth中,我不小心将index.html中的base href从“/”更改为“”
const routes: Routes = [
  {path:'dashboard',component:DashboardComponent,canActivate: [AuthGuard]},
  {path:'section',component:SectionComponent,canActivate: [AuthGuard]},
  {path:'subsection',component:SubsectionComponent,canActivate: [AuthGuard]},
  {path:'equipment',component:EquipmentComponent,canActivate: [AuthGuard]},
  {path:'downtime',component:DowntimeComponent,canActivate: [AuthGuard]},
  {path:'user',component:UserComponent,canActivate: [AuthGuard],data:{allowedRoles:['Admin']}},
  {path:'mainfailure',component:MainfailureComponent,canActivate: [AuthGuard]},
  {path:'failureaction',component:FailureactionComponent,canActivate: [AuthGuard]},
  {path:'failuremechanism',component:FailuremechanismComponent,canActivate: [AuthGuard]},
  {path:'failuremode',component:FailuremodeComponent,canActivate: [AuthGuard]},
  {path:'failurecause',component:FailurecauseComponent,canActivate: [AuthGuard]},
  {path:'login',component:LoginComponent},
  {path:'forbidden',component:ForbiddenComponent},
  {path:'',redirectTo:'dashboard',pathMatch:'full'},
  {path:'**',redirectTo:'dashboard'}
];