1

I know this question asked many time i have search many SO question but unable to find answer so just posting my question my structure is like below.

const appRoutes: Routes = [
    {
        path: '',
        redirectTo: 'A',
        pathMatch: 'full'
    },
    {
        path: 'A',
        resolve: {
            DATA: ADataResolver,
        },
        children: [
            {
                path: '',
                redirectTo: 'B',
                pathMatch: 'full'
            },
            {
                path: 'B',
                loadChildren: './b/b.module#BModule'
            }]
    }
]

now on app load i want to load path A/B

How will i do it ? I have done above thing and its not working. Any help is appreciated.

4
  • Any error? have you added router-outlet ? Commented Feb 20, 2018 at 6:13
  • 'A' is a Module and 'B' is a component of module A? Commented Feb 20, 2018 at 6:13
  • @SurenSrapyan yes i have added when i directly put url things works Commented Feb 20, 2018 at 6:36
  • @AkashTantri No they both are module Commented Feb 20, 2018 at 6:36

3 Answers 3

4

I wonder if something else is wrong somewhere else. I just modified my code to (basically) match what you have and it worked fine (see below). I have movies, so that would be your "B".

const appRoutes: Routes = [
    {
        path: '',
        redirectTo: 'A',
        pathMatch: 'full'
    },
    {
        path: 'A',
        children: [
            { path: 'welcome', component: WelcomeComponent },
            {
                path: 'movies',
                loadChildren: './movies/movie.module#MovieModule'
            },
            { path: '', redirectTo: 'movies', pathMatch: 'full' }
        ]
    },
    { path: '**', component: PageNotFoundComponent }
];

Navigating to http://localhost:4200 resulting in a URL of: http://localhost:4200/A/movies

You can find my code here: https://github.com/DeborahK/MovieHunter-communication/tree/master/MH-Take4

I just modified my app-routing.module.ts as shown above and ran it.

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

3 Comments

Have you tried to run this code bse i have setup same thing and its not working :(
Did you try to run the code from my github? It all works. I just used it in a talk I gave at a conference last week and it worked with no problem.
@DeborahK can you please have a look at my question: stackoverflow.com/questions/57357145/…. I'm having same issue with multiple lazy-loading modules.
1

Please check B module it's also required component and routing concept.

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CommonModule } from '@angular/common';
import { AdminComponent } from "../components/admin/admin.component";

export const routes: Routes = [
  {
    path: '', component: AdminComponent 

  }
];

@NgModule({
  declarations: [
    AdminComponent
  ],
  imports: [
    CommonModule, 
     RouterModule.forChild(routes)
  ],
  bootstrap: [AdminComponent]
})
export class BModule { }

2 Comments

its already done But when i enter A it will redirect to A/B but i want by default it will goes to A/B
@Riturajratan plz try: const appRoutes: Routes = [ { path: '', redirectTo: 'A/B', pathMatch: 'full' },
0

Lazy loading should be used when you want to delay the loading of one or more modules until the user requests those modules. Looks like your scenario needs the B module by default. You could directly invoke the component:

const appRoutes: Routes = [
    {
        path: '',
        redirectTo: 'A',
        pathMatch: 'full'
    },
    {
        path: 'A',
        resolve: {
            DATA: ADataResolver,
        },
        children: [
            {
                path: '',
                redirectTo: 'B',
                pathMatch: 'full'
            },
            {
                path: 'B',
                component: 'BComponent'
            }]
    }
]

If it still has to be a lazy module, did you try enableTracing? Is the route getting recognized?

2 Comments

But when i enter A it will redirect to A/B but i want by default it will goes to A/B
const appRoutes: Routes = [ { path: '', redirectTo: 'A', pathMatch: 'full' }, {path: 'A/B', resolve: { DATA: ADataResolver, }, component: 'BComponent'}]

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.