简体   繁体   English

Header 组件可以添加到 HTML Angular 8 的每个组件中

[英]Header component can be added in to every component HTML Angular 8

I am trying to implement role guard as per the below url http://coding-karma.com/2018/06/17/role-based-access-control-in-angular-4-route-guards-in-depth/我正在尝试按照以下网址实现角色保护http://coding-karma.com/2018/06/17/role-based-access-control-in-angular-4-route-guards-in-depth/

app.component.html has the following code app.component.html 有以下代码

 <app-header></app-header> <router-outlet></router-outlet> <app-footer></app-footer>

I have given the role checking in header like below我已经在标题中进行了角色检查,如下所示

 export class HeaderComponent implements OnInit { constructor( private authService: authService, private router: Router) { const token = localStorage.getItem('jwt'); const tokenPayload = decode(token); console.log("tokenPayload.role", tokenPayload.role); this.Role = tokenPayload.role; } getRoleB(){ if(this.Role=='FA' || this.Role=='FTH'){ return true; } else{ return false; } } getRoleC(){ if(this.Role=='FA' || this.Role=='FTH' || this.Role=='FT'){ return true; } else{ return false; } } }

Also after the successful login its going to the respective paths like below同样在成功登录后,它会转到如下所示的相应路径

 const token = localStorage.getItem('token'); // decode the token to get its payload const tokenPayload = decode(token); console.log(tokenPayload); if (tokenPayload['user_role'] == "user"){ this.router.navigate(['/dashboard']) } else { this.router.navigate(['admin/dashboard']) }

I have only one problem.我只有一个问题。 After successful login the router navigate to respective dashboards, but header it will show only common links.成功登录后,路由器导航到相应的仪表板,但标题将仅显示常见链接。 If i refresh the page everything works fine.如果我刷新页面一切正常。

If i place the header in the dashboard it works fine.如果我将标题放在仪表板中,它工作正常。 I would like to know is this the right approach ?我想知道这是正确的方法吗? adding the header in each component separately?分别在每个组件中添加标题? Please suggest..请建议..

Ideally you should have app module which will render header,content and footer.理想情况下,您应该有应用程序模块来呈现页眉、内容和页脚。 and You should have dashbaord as a children to app module which will be loaded through route /dashboard.并且您应该将 dashbaord 作为 app 模块的子项,该模块将通过路由 /dashboard 加载。 So dashboard should be your lazy loaded children module as shown below所以仪表板应该是你懒加载的子模块,如下所示

const routes: Routes = [
  {
    path: 'dashboard',
    loadChildren: './dashboards/dashboards.module#DashboardsModule'
  }
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];

@NgModule({
  imports: [
    RouterModule.forRoot(routes)
  ],
  exports: [RouterModule],
  providers: []
})
export class AppRoutingModule { }

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

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