Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

angular2 routing - Conditionally Load Different Modules on the Same Path in Angular

I have a multiple user Angular app, with a module for each user (because they have totally different accessible pages), like this:

  • app.module.ts
  • app-routing.module.ts
  • login/
    • login.component.ts
  • admin/
    • pages/
      • user-management/
      • configuration/
    • admin.module.ts
    • admin-routing.module.ts
  • user/
    • pages/
      • task-management/
      • configuration/
    • user.module.ts
    • user-routing.module.ts
  • guest/
    • pages/
    • guest.module.ts
    • guest-routing.module.ts

From the login page (/login from app-routing), I want to redirect to each module based on the credentials the user provided, but NOT with children routes like /user /admin or /guest

Instead, when an admin logs in, I want the URL to be reset. So, for example, the admin should not see the paths /admin/user-management or /admin/configuration; it just accesses /user-management or /configuration

Is this possible? Will it be a problem if I have a /configuration route for both admin and user?

EDIT: Here's a Stackblitz working example. Take a look at the URL routes when logged in.

EDIT 2: In the Stackblitz example, you can see the original problem on master and the working solution on solution branches.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

SOLUTION:

After a lot of investigation, I found out that it can be done with creating a route matcher.

CODE:

I managed to make it work with your code. Here is the working example: https://stackblitz.com/edit/angular2-routing-test-qj4geu?file=src/app/user/user.component.html

This is the relevant code on app-routing.module.ts:

const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent
  },
  {
    matcher: url => {
      const user_type = localStorage.getItem('user_type');
      if (user_type === 'user') {
        return url.length ? { consumed: [] } : { consumed: url };
      }
      return null;
    },
    loadChildren: () => import('./user/user.module').then(m => m.UserModule)
  },
  {
    matcher: url => {
      const user_type = localStorage.getItem('user_type');
      if (user_type === 'admin') {
        return url.length ? { consumed: [] } : { consumed: url };
      }
      return null;
    },
    loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)
  },
  {
    matcher: url => {
      const user_type = localStorage.getItem('user_type');
      if (user_type === 'guest') {
        return url.length ? { consumed: [] } : { consumed: url };
      }
      return null;
    },
    loadChildren: () => import('./guest/guest.module').then(m => m.GuestModule)
  },
  {
    path: '',
    pathMatch: 'full',
    redirectTo: 'login'
  }
];

NOTE:

As stated here: https://github.com/angular/angular/issues/23866#issuecomment-388527483, when we use route matcher we need to specify which portion of the url was consumed by the matcher function, and the remaining portion will be send to the nested router.

BLOGS:

Here are two blog posts that has in-depth details and walk-through how to do it:

Blog 1: https://medium.com/@brandontroberts/custom-route-matching-with-the-angular-router-fbdd48665483

Blog 2: https://medium.com/@lenseg1/loading-different-angular-modules-or-components-on-routes-with-same-path-2bb9ba4b6566


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...