1
votes

I have some trouble with child route-outlet. I have dashboard module and child modules as profile, staff etc. But profile component loads not only on link localhost:4200/dashboard/profile, but just localhost:4200/profile too. Seems like child component loads to root route-outlet

here is the example only with login (but it the same project and configuration also the same), works as idr-frontend.staging.myappworx.com/auth/login and idr-frontend.staging.myappworx.com/login

1. App.module

 import {routing} from './app.routing';
    import {DashboardModule} from './dashboard/dashboard.module';
    import {AppComponent} from './app.component';

    @NgModule({
        declarations: [
            AppComponent
        ],
        imports: [
           routing,
           DashboardModule
        ]
    })
    export class AppModule {}

2. App.routing.ts

import {Routes, RouterModule} from '@angular/router';
import {DashboardComponent} from './dashboard/dashboard.component';
import {LoginRedirect} from './auth/services/login-redirect.service';
import {EnsureAuthenticated} from './auth/services/ensure-authenticated.service';
const appRoutes: Routes = [         
   {
        path: 'dashboard',
        component: DashboardComponent,
        canActivate: [EnsureAuthenticated],
        loadChildren: './dashboard/dashboard.module#DashboardModule',
    },
    {
        path: '**',
        redirectTo: 'dashboard',
        canActivate: [EnsureAuthenticated]
    }
];

export const routing = RouterModule.forRoot(appRoutes);

3. app.component.html

<router-outlet></router-outlet>

4. dashboard.module.ts

import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {DashboardComponent} from './dashboard.component';
import {dashboardRouting} from './dashboard.routing';
import {ProfileModule} from '../profile/profile.module';
import { StaffModule } from '../staff/staff.module';
@NgModule({
    imports: [
        CommonModule,
        FormsModule,
        dashboardRouting,
        ProfileModule,
        StaffModule
    ],
    declarations: [
        DashboardComponent
    ],
    providers: [            
    ],
    exports: []
})
export class DashboardModule {}

5. dashboard.routing.ts

import {Routes, RouterModule} from '@angular/router';
import {ProfileComponent} from '../profile/profile.component';
import {EnsureAuthenticated} from '../auth/services/ensure-authenticated.service';

import { StaffComponent } from '../staff/staff.component';
const dashboardRoutes: Routes = [
    {
        path: 'profile',
        component: ProfileComponent,
        canActivate: [EnsureAuthenticated]
    },
    {
        path: 'staff',
        component: StaffComponent,
        canActivate: [EnsureAuthenticated]
    }
];

export const dashboardRouting = RouterModule.forChild(dashboardRoutes);

6. dashboard.component.html

   <div class="contentWrap">
        <header class="header">

        <div class="currentTime">
            {{currentTime}}
        </div>
        <div class="status"></div>

    </header>
    <router-outlet></router-outlet>
</div>
2
with that configuration, /profile wouldnt work, only /dashboard/profile would, unless its cached a version where /profile was valid?mast3rd3mon
Could you make a stackblitz out of your example ?ibenjelloun
here is the example only with login (but it the same project and configuration also the same), works as idr-frontend.staging.myappworx.com/auth/login and idr-frontend.staging.myappworx.com/loginOlga Matosova

2 Answers

2
votes

please delete DashboardModule from your imports in app.module.ts because it is lazy loaded, maybe that's why you have /profile working too

 imports: [
       routing,
       DashboardModule // delete this
    ]
0
votes

The reason why localhost:4200/profile also loads your child component is this:

 { path: '**', redirectTo: 'dashboard', canActivate: [EnsureAuthenticated] }

** wild card is used to redirect paths which don't match any of the routes defined earlier in the configuration, and as I can see you didn't define profile path, therefore you are redirected to /dashboard.

** is useful for redirecting to 404 page

Read more about it in Official Documentation