Angular Route LazyLoading匹配路由前缀以加载子级

时间:2018-09-27 12:34:37

标签: angular angular-routing angular-router

我正在尝试对不同的模块进行延迟加载,并且试图将路由前缀与加载模块进行匹配。但它不起作用,它适用于确切的URL,但不适用于前缀。我正在使用角度6.1。

此代码无效

{
    path: 'account',
    component: AuthComponent,
    children: [
        {
            path: '',
            loadChildren: './modules/auth/auth.module#AuthModule'
        }
        {
            path: '**',
            loadChildren: './modules/auth/auth.module#AuthModule'
        }
    ]
},

,此代码可以正常工作

{
    path: 'account',
    component: AuthComponent,
    children: [
        {
            path: '',
            loadChildren: './modules/auth/auth.module#AuthModule'
        },
        {
            path: 'login',
            loadChildren: './modules/auth/auth.module#AuthModule'
        },
        {
            path: 'register',
            loadChildren: './modules/auth/auth.module#AuthModule'
        },
        {
            path: '**',
            loadChildren: './modules/auth/auth.module#AuthModule'
        }
    ]
},

我必须为加载模块映射每条路线吗?

这些是我要加载的模块中的路由。

const routes = [
{
    path: '',
    component: LoginComponent,
    data: {
        title: 'login'
    }
},
{
    path: 'login',
    component: LoginComponent,
    data: {
        title: 'login'
    }
},
{
    path: 'register',
    component: RegisterComponent,
    data: {
        title: 'register'
    }
},
{
    path: 'admin/login',
    component: LoginComponent,
    data: {
        title: 'Admin login'
    }
}
 ];

谢谢。

1 个答案:

答案 0 :(得分:1)

尝试这样:

MainModule

{
    path: 'account', loadChildren: './modules/auth/auth.module#AuthModule'
},

AuthModuleRouting

const routes = [
{
    path: '', component: AuthComponent,
},
{
    path: 'login',
    component: LoginComponent,
    data: {
        title: 'login'
    }
},
{
    path: 'register',
    component: RegisterComponent,
    data: {
        title: 'register'
    }
},
{
    path: 'admin/login',
    component: LoginComponent,
    data: {
        title: 'Admin login'
    }
}
 ];