Thursday, 10 September 2020

Angular Route, app module route to children moudle route to children compoennt

 app.module

import { RouterModule, Routes } from '@angular/router';


const routes: Routes = [

    {

        path: '',

        canActivateChild: [YOURCUSTOMGUARD],

        children: [

            {

                path: 'test',

// @test path specified in tsconfig.js "paths", lazy load module on root level(specified below), actual path is equalty to parent('') + '/' + children so /test

                loadChildren: () => import('@test/test.module').then(mod => mod.TestModule),

            },


...}



@NgModule({

    declarations: [

....

    ],

    imports: [

// Enable /test route at root level, and uses hash mode on host, so it will be host/#/test

        RouterModule.forRoot(routes, { useHash: true }),

]


------------------------------------------------


test.module

import { RouterModule, Routes } from '@angular/router';

import {TestCreateComponent } ...

const routes: Routes = [

    {

        path: '',

        children: [

            {

                path: 'create'

                component: TestCreateComponent

            },


...}


@NgModule({

    declarations: [

....

    ],

    imports: [

// Enable /test/create at module level, 

            RouterModule.forChild(routes),

]



Trigger:

since parent is hash mode, yourhost/#/test will load testModule from app.module, /test/create will load TestCreate Component in test module

No comments:

Post a Comment