如何路由到另一个模块组件

问题描述 投票:0回答:1

我有两个模块:

app.module.ts
auth.module.ts
auth.module.ts
有两个组成部分:
login.components.ts
registration.component.ts

我希望

login.components.ts
处理路线
www.mywebsite.com/login
,而
registration.component.ts
处理路线
www.mywebsite.com/registration

如果我使用延迟加载路由

app-routing.module.ts

  {
    path: 'login',
    loadChildren: () => import('../auth/auth.module').then((m) => m.AuthModule),
  },

auth-routing.module.ts

    {
        path: 'login',
        component: LoginComponent
    },

然后路径就变成了

www.mywebsite.com/login/login

如何使

www.mywebsite.com/login
login.component.ts
中的
auth.module.ts
处理,以及
www.mywebsite.com/registration
registration.component.ts
中的
auth.module.ts
处理?

StackBlits 项目

旁注:

我还尝试直接在

LoginComopnent
 中使用 
auth.module.ts
 中的 
app-routing.module.ts

app-routing.module.ts

import { LoginComponent } from '../auth/login/login.component';
const routes: Routes = [
  {
    path: 'login',
    component: LoginComponent
  },
];

但这导致了

Can't bind to 'formGroup' since it isn't a known property of 'form'.
错误,甚至很难,我在
FormsModule
ReactiveFormsModule
模块中都导入了
app.module.ts
auth.moduel.ts

angular angular-routing
1个回答
0
投票

如果你没有模块,就这个。如果您有父路线,则不会使子路线具有相同的名称

 {
  path: 'login',
  children: [
     {
      path: 'login',
      component: LoginComponent,
    },
  ]
}

当然,这会导致

/login/login
路线。相反,你会留下一个未命名的,就像这样:

 {
  path: 'login',
  children: [
     {
      path: '',
      component: LoginComponent,
    },
  ]
}

结果是

/login

因此,对于您的情况,对于嵌套模块,我认为您必须为其命名。我推荐

auth
,像这样

{
    path: 'auth',
    loadChildren: () => import('../auth/auth.module').then((m) => m.AuthModule),
  },

然后您就有

auth/login
auth/registration
路线

参见 StackBlitz

© www.soinside.com 2019 - 2024. All rights reserved.