3

As I understand, the angular router lazy loading works from a url segment.

For example in the live example of docs, you have crisis-center in a separate module and the routes from his components hang of ‘www.server.org/crisis-center/’…

But what about if you want to have the crisis-center components without hanging on ‘www.server.org/crisis-center’ url segment??

For example ‘www.server.org/crisis-center’ pointed to CrisisListComponent and ‘www.server.org/crisis-admin’ pointed to CrisisAdminComponent

I can’t find a way to point the two routes to the same lazy loaded module …

Someone knows any way to use lazy loading without lost a url sement?

Other example:

Let's see this question: Multiple Components from Same Lazy Route Not Working

The solution is to make a single lazy route:

{ path: 'Lazy', loadChildren: './+lazy/lazy.module#LazyModule' }

Then you can use www.asdf.com/Lazy/Page30 and www.asdf.com/Lazy/Page31, the first segment of the url (lazy) is lost...

Is there some way to use www.asdf.com/Page30 and www.asdf.com/Page31 using lazy loading with the same lazy module??

2
  • what do you mean by ` hang of `? Commented Feb 22, 2017 at 12:54
  • "to be inside of", "hanging on" Commented Feb 22, 2017 at 13:03

3 Answers 3

1

There are 3 possible solutions (workarounds) I know of:

  1. Separate lazy module into 2 lazy modules with separate routing - different urls usually mean that those components do separate things, and should be in their own modules
  2. Add a prefix before those components - instead of url/crisis-admin and url/crisis-user you could do url/crisis/admin and url/crisis/user. (or url/crisis/crisis-admin if that looks nicer to you)
  3. Route / to module, so you can declare further routing there (you probably don't want to do it)

In the example with pages you should separate it like this:

url/page //base url
----url/page/30
----url/page/31

Or even better:

url/page?number=30
url/page?number=31

And fit those pages into single component (if that is feasible for you)

Sign up to request clarification or add additional context in comments.

Comments

1

I was trying the same thing and I have found a solution.

In app.module routing you can do something like

{
path: '',
component: UserLayoutComponent,
children: [
  {path: '', component: HomeComponent},
  {
    path: '',
    loadChildren:'app/user/user/user.module#UserModule'
  }
]
}

Now the UserModule is lazy loaded

and in User Module routing can go like this

const routes: Routes = [
{ path: 'profile', component: ProfileComponent },
{ path: 'edit', component: EditProfileComponent} 
];

likewise profile and edit will work like : localhost:8080/profile and localhost:8080/edit

To check if the module is actually lazy loading put some console.log() inside module's constructor.

Hope this helps, thanks

Comments

0

You can have a router config like this one:

const routes: any = [{
    path: "crisis-center",
    loadChildren: "crisis.module#CrisisModule"
}, 
{ 
    path: "crisis-admin", 
    loadChildren: "crisis.module#CrisisModule"
}];
export const appRoutes: ModuleWithProviders = RouterModule.forRoot(routes, { useHash: false});

In your child router config (must be imported by your crisis-center module):

const routes: Routes = [{
    path: "crisis-center",
    component: CrisisListComponent,
    pathMatch: "full"
},{
    path: "crisis-admin",
    component: CrisisAdminComponent,
    pathMatch: "full"
}];

export const childRoutes: ModuleWithProviders = RouterModule.forChild(routes);

This is just a long shoot, I didn't tried it yet.

5 Comments

no, this match with "crisis-center/crisis-center", "crisis-center/crisis-admin", "crisis-admin/crisis-admin" and "crisis-admin/crisis-center"
Thanks for the Proof of Concept, it's seems better to have a common module, crisisCenter in this case and create two modules, one for admins and another one for visitors, just import common module and use the componentes and services that you want and that's all.
yes, but no XD. In large applications you can have to many routes for each module. For example: WarehouseModule -> Invoice, DeliveryNote, Supplier, Sales, ... CustomerModule -> Customer, Company, Addresses, Comments, ... StreetPanModule: Country, Region, Town, PostalCode, Street, ... I would like to have routes like app/supplier, app/street, app/invoice ... But this router force you to have app/wh/invoice, app/cus/addresses, app/steetplan/contry, ... I'm becoming a router hater ...
Yeah, I know that pain. I must admit, I don't like it too. But it's better to find a fancy name for the module than wait to download 8mb of app in a single module. Let's see what the angular team will do with lazy loading and route paths.
Yes, I know, lazy loading is needed. I'm with angular 2 from beta phase and I know the Router 2.0, do you know? I like 2.0 more than 3.0. I don't know the reasons for change it, perhaps because it was written by Rob Eisemberg that go out to Aurelia Project ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.