【问题标题】:Angular 9 children route module not loadingAngular 9子路由模块未加载
【发布时间】:2020-07-26 12:47:55
【问题描述】:

我正在使用 Angular 9 创建一个应用程序,其中我为管理目的创建了一个单独的路由模块,并在 app.module 中调用它进行初始化。但是由于某种原因,路由没有被调用,并且控制台中出现了以下错误。

ERROR 错误 角 11 解决承诺 解决承诺 调度解决或拒绝 调用任务 onInvokeTask 调用任务 运行任务 排水微任务队列 调用任务 调用 计时器 core.js:3872

我在以前版本的 Angular 中创建了这样的子模块,并且效果很好。如果您想https://github.com/tridibc2/sample-angular,可以查看我的 github 存储库。看看我下面的模块

管理模块:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Routes, RouterModule } from '@angular/router';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AdminComponent } from '../admin/admin.component';
import { SignupComponent } from '../signup/signup.component';
import { LoginComponent } from '../login/login.component';
import { ManageBlogsComponent } from '../manage-blogs/manage-blogs.component';

const routes: Routes = [
  { path: 'signup', component: SignupComponent },
  { path: 'login', component: LoginComponent },
    { path: 'admin', component: AdminComponent },
   { path: 'admin/blog', component: ManageBlogsComponent }
];

@NgModule({
  declarations: [
    AdminComponent,
    SignupComponent,
    LoginComponent,
    ManageBlogsComponent
  ],
  imports: [
    RouterModule.forChild(routes),
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
  exports: [RouterModule]
})
export class AdminModule { }

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { RouterModule } from '@angular/router';
import { ClientModule } from './client/client-routing/client.module';
import { AdminModule } from './admin/admin-routing/admin.module';
import { AppRoutingModule } from './app.routing';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent ],
  imports: [
    BrowserModule,
    CommonModule,
    NgbModule,
    ClientModule,
    AdminModule,
    AppRoutingModule,
    FormsModule,
    RouterModule ],

   bootstrap: [AppComponent]
})
export class AppModule { }

app.routing.ts:

import { NgModule } from '@angular/core';
import { CommonModule, } from '@angular/common';
import { BrowserModule  } from '@angular/platform-browser';
import { Routes, RouterModule } from '@angular/router';
import { BlogHomeComponent } from './client/blog-home/blog-home.component';
const routes: Routes =[
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: 'home',             component: BlogHomeComponent }
];

@NgModule({
  imports: [
    CommonModule,
    BrowserModule,
    RouterModule.forRoot(routes,{
      useHash: true
    })
  ],
  exports: [
  ],
})
export class AppRoutingModule { }

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    预加载模块路由的简单示例。 父模块中至少需要一个组件的 Ref,并且该组件主要是具有 router-outlet 标记以呈现子模块路由的组件。

    ...
    
    const routes: Routes = [
      { 
        path: '', 
        component: CompWhichHasRouterOutletTag,
        children:[
          {path: '', redirectTo: 'signup', pathMatch: 'full'},
          { path: 'signup', component: SignupComponent },
          { path: 'login', component: LoginComponent },
          { path: 'admin', component: AdminComponent },
          { path: 'admin/blog', component: ManageBlogsComponent }]
        }
    
    ];
    
    @NgModule({
      declarations: [
        CompWhichHasRouterOutletTag,
        AdminComponent,
        SignupComponent,
        LoginComponent,
        ManageBlogsComponent
      ],
      imports: [
        RouterModule.forChild(routes),
        CommonModule,
        FormsModule,
        ReactiveFormsModule
      ],
      exports: [RouterModule, CompWhichHasRouterOutletTag]
    })
    export class AdminModule { }
    

    app.routing.ts 文件

    const routes: Routes = [
      { path: '', redirectTo: '/home/', pathMatch: 'full' },
      { path: 'home', component: HomeComponent },
      { path: 'contact', component: ContactComponent },
      { path: 'admin-route', component: CompWhichHasRouterOutletTag },
    ]
    @NgModule({
      imports: [
        ...
    
        RouterModule.forRoot(routes),
    
        ...
      ]
    })
    
    

    【讨论】:

    • 在 AdminRoutingModule 的导出中添加具有路由器出口的 adminComponent 在控制台中获取此错误----未处理的承诺拒绝:无法从 AdminRoutingModule 导出组件 AdminComponent,因为它既没有声明也没有导入!
    • 为了访问一个模块组件 |管道 |您必须从该模块导出的指令。这就是为什么我只从 AdminRoutingModule 导出 CompWhichHasRouterOutletTag 并在 app、routes.ts 中使用 admin-route 来启动 AdminRoutingModule 中的路由,它是子组件
    • 但它不能以这种方式工作..注意到进入视图并且上面的错误出现在控制台上。我可以在github上分享你想要检查的代码
    • @surjendu_dey 分享一个回购链接。
    猜你喜欢
    • 2020-07-26
    • 2017-01-07
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    相关资源
    最近更新 更多