【问题标题】:Angular Routing views for different users不同用户的角度路由视图
【发布时间】:2018-05-16 14:57:45
【问题描述】:

我正在尝试在我的 Angular 4 应用程序中为 3 种类型的用户设置路由。

一旦用户登录,他将被重定向到我的应用组件。这就是我的 app-component.html 的样子:

<div class="main-container">
  <div class="wrapper">
    <router-outlet></router-outlet>
  </div>
</div>

我想检查用户的类型并据此 - 在我的路由器插座中打开不同的路由。为了给你一个想法,我将向你展示我想要的结构:

index.html
>app-component
>register-component
>login-component
>dentist-view
  schedule component
  patients component
  profile component
  appointments component  etc..
>admin-view
  manage users component
>patient-view
  ambulatory-lists component
  profile component
  dentist search component
  book appointment component etc..

因此,根据用户类型,我想加载完全不同的视图,这些视图在我的项目中的结构类似。我希望每个用户使用不同的导航栏,不同的编辑配置文件组件等。实现此目的的正确方法是什么,因为一旦登录,我将收到重定向到应用程序组件的用户类型,因此我将能够发送它的孩子 - 牙医视图组件,患者视图组件等。

为了给你一个更好的主意,比如一个替代方案,但路由会很棒:(免责声明:我知道这是不可能的:D) 在 app.component.html 中:

<div class="main-container">
  <div class="wrapper">
    <router-outlet>
     <dentist-component *ngIf="isDentist()"></dentist-component>
     <patient-component *ngIf="isPatient()"></patient-component>
     <admin-component *ngIf="isAdmin()"></admin-component>
   </router-outlet>
  </div>
</div>

由于我是新手并且仍在弄清楚事情,所以我想知道我是朝着正确的方向前进还是走错了路。任何建议都将不胜感激。

【问题讨论】:

    标签: angular typescript authentication routing


    【解决方案1】:

    此答案基于@abdul hammed answer 更多信息Angular.io documentation

    守卫是解决方案(guard.service):

        import { Injectable }     from '@angular/core';
        import { CanActivate }    from '@angular/router';
        import { Router } from '@angular/router';
    
        @Injectable()
        export class Guard implements CanActivate {
          canActivate() {
    
        if (this.user && this.user.profile.role == 'Dentist') {
                 this.router.navigate(['dentist']);
            } if else (this.user && this.user.profile.role == 'Patient') {
                this.router.navigate(['patient']);
            } ...
    
            return true;
          }
    
     constructor(private router: Router) { }
        }
    

    你必须更新你的 app.module 文件,

    import { Guard } from './_services/guard.service';
    import { DentistComponent } from './dentist/dentist.component';
    import { PatientComponent } from './dentist/patient.component';
    @NgModule({
      declarations: [
        AppComponent,
        DentistComponent,
        PatientComponent
      ],
      imports: [
        BrowserModule,
        HttpModule,
    
        RouterModule.forRoot([
            {
              path: 'dentist',
              component: DestistComponent,
              canActivate:[Guard]
            },
            {
            path: 'patient',
            component: PatientComponent,
            canActivate:[Guard]
           }
        ])
      ],
      providers: [Guard],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    【讨论】:

      猜你喜欢
      • 2014-07-19
      • 2021-04-22
      • 2020-04-04
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 2014-08-29
      • 2019-10-19
      • 1970-01-01
      相关资源
      最近更新 更多