简体   繁体   English

Angular 路由总是在组件之前显示 url

[英]Angular Routing always show url before component

I don't know what happen, but in my Angular 7 apps when i input some url via browser it's always following url that i'm typing.我不知道会发生什么,但是在我的 Angular 7 应用程序中,当我通过浏览器输入一些 url 时,它总是跟随我正在输入的 url。 Example:例子:

typing => http://localhost:4200/xxx打字 => http://localhost:4200/xxx

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

typing => http://localhost:4200/ooo打字 => http://localhost:4200/ooo

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

in RouteTracing url xxx or ooo cannot be read, NavigationStartUrl it's always "/" (image attached)在 RouteTracing url xxxooo中无法读取,NavigationStartUrl 始终为“/”(附图)

here's my AuthGuard:这是我的 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;
    }
}

and here's is my AppRoute:这是我的 AppRoute:

const routes: Routes = [
  {path:'',redirectTo:'dashboard',pathMatch:'full'},
  {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'}
];

i enable tracing in routes but every xxx that's i input to browser always say it's "/" here's the log: Routes Log我在路由中启用了跟踪,但是我输入到浏览器的每个 xxx 总是说它是“/”这是日志: Routes Log

This guide for Setting up redirects , from the official Angular website, puts the redirect after the target redirect route and before the wildcard route, so maybe you should try this:本指南设置重定向,来自官方 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'}
];

thanks, well after search so long, the problem is not in routemodule or auth, i accidently change base href in index.html from '/' to ''谢谢,经过这么长时间的搜索,问题不在于 routemodule 或 auth,我不小心将 index.html 中的 base href 从“/”更改为“”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM