0

Before I upgraded to angular-cli.beta-24 I had this route structure:

+route1
 - +subroute1
 - +subroute2
 - +subroute3

But this fails when having the subroutes defined in the router.module of +route1 because they can't be found now that AOT is enabled by default.

I can get it working by not having the subroutes defined there, and instead moving them to the same router.module as all the other routes in the app. Instead looking like this:

+route1
+subroute1
+subroute2
+subroute3

With the routes obviously not being subroutes anymore.

While this works, it's not a viable solution for me since my breadcrumbs relies on the router tree and it just creates a mess overall if you can't have a router tree anymore.

Here's an example (route1's router.module file):

const routes: Routes = [
  {
    path: '',
    component: Route1Component
  },
  {
    path: 'subroute1',
    loadChildren: '+subroute1/subroute1.module#Subroute1Module'
  },
  {
    path: 'subroute2',
    loadChildren: '+subroute2/subroute2.module#Subroute2Module'
  },
  {
    path: 'subroute3',
    loadChildren: '+subroute3/subroute3.module#Subroute3Module'
  }
];

Why can't you define routes like this when using AOT? What am I missing?

Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175

1 Answers1

2

AOT and lazy loading works well together since BETA.21 (I made a post about that).

Instead of passing to loadChildren a relative path from . you should start from the app folder like that :

const routes: Routes = [
  {
    path: '',
    component: Route1Component
  },
  {
    path: 'subroute1',
    loadChildren: 'app/+route1/+subroute1/subroute1.module#Subroute1Module'
  },
  {
    path: 'subroute2',
    loadChildren: 'app/+route1/+subroute2/subroute2.module#Subroute2Module'
  },
  {
    path: 'subroute3',
    loadChildren: 'app/+route1/+subroute3/subroute3.module#Subroute3Module'
  }
];

EDIT 1 : It could come from your barrel.

There was some trouble with beta.23 and they did jump to beta.24. BUT a breaking change was introduced in beta.23 and if you read only the changelog for beta.24 you might have missed it. Please take a look to Beta 23 changelog, breaking changes :

blueprints: The app root module and component must now be imported directly. (e.g., use import { AppModule } from './app/app.module'; instead of import { AppModule } from './app/';)

If I understood well, barrels are not working with AOT (which is now enabled by default). Basically, you can remove index.ts and should import what you need directly.

Community
  • 1
  • 1
maxime1992
  • 22,502
  • 10
  • 80
  • 121
  • Already tried this without luck. Thing is that some of the child routes work but never when the lazy loading is 3 levels deep. Because starting from my `AppModule`, I've got another level with `AdminAreaModule`, which has a `DashboardModule`, but beyond that one it doesn't work.. Despite doing the exact same definitions. – Chrillewoodz Jan 02 '17 at 12:07
  • Could it be my barrel imports that cause issues? – Chrillewoodz Jan 02 '17 at 14:13
  • I don't understand "barrel imports". What do you mean by that ? – maxime1992 Jan 02 '17 at 15:37
  • 1
    Barrel is a redundant, meaningless term that someone in the angular world came up with for describing the convention of an `index` file that ex-exports the contents of its containing directory. There was no need for such a term and hopefully people will stop using it. – Aluan Haddad Jan 02 '17 at 18:51
  • Ohh I see, thanks @AluanHaddad ! I made an edit to my post because it might come from a barrel indeed ! – maxime1992 Jan 03 '17 at 07:54
  • 1
    Currently changing thousands of imports lol, I'll get back to you when my fingers stop bleeding. – Chrillewoodz Jan 03 '17 at 10:08
  • Yep those changes can be quite annoying and might need a ton of work but it's the game with beta version right ? :) Good luck with that, I hope the problem comes from there ... ! – maxime1992 Jan 03 '17 at 10:54
  • No it's still a problem and it's a massive design flaw. Don't even get me started on the opaque token function or the type interface. If these things sound weird it's because they are. The designed the framework is fundamentally at odds with both the tools and is built with and the platform it targets. They try to compile away decorators and reify types. – Aluan Haddad Jan 03 '17 at 13:34
  • Turns out it's a bug in the latest version of the angular cli, I added a temporary solution to get it going. A pull request has been added to fix it and hopefully it'll be released next week. – Chrillewoodz Jan 04 '17 at 14:00
  • Can you link the issue ? Just curious :) – maxime1992 Jan 04 '17 at 14:08