代码之家  ›  专栏  ›  技术社区  ›  JSON

为什么我的Angular应用程序会将我的有效路线识别为无效?

  •  0
  • JSON  · 技术社区  · 7 年前

    我有两个基本的 NgModules 在这个应用程序中,核心(用于页眉、页脚和主页)和 auth 基本上用于身份验证。不使用 wildcard 该应用程序可以在 components . 一旦我引入无效路由,唯一加载的组件就是主组件。 我从我的头组件路由,即。 routerLink="/signin" 知道为什么会这样吗?

    以下是我的代码,

    核心模块

    @NgModule({
        declarations: [
          HeaderComponent,
          FooterComponent,
          SidenavLeftComponent,
          HomeComponent
        ],
        imports: [
          CommonModule,
          BrowserModule,
          BrowserAnimationsModule,
          MDBBootstrapModule.forRoot(),
          MDBBootstrapModulePro.forRoot(),
          NgbModule.forRoot(),
          AppRoutingModule
        ],
        exports: [
          HeaderComponent,
          FooterComponent,
          SidenavLeftComponent,
          HomeComponent,
          AppRoutingModule
            ],
        schemas: [ NO_ERRORS_SCHEMA ]
      })
      export class CoreModule { }
    

    批准模块

    const appRoutes: Routes = [
        { path: '', redirectTo: 'home' , pathMatch: 'full' },
        { path: 'home', component: HomeComponent },
        { path: 'not-found', component: NotFoundComponent, data: { message: 'We Could Not Serve Your Request!'}},
        { path: '**', redirectTo: '/not-found',  pathMatch: 'full'}
    ];
    @NgModule({
        imports: [
          RouterModule.forRoot(appRoutes, {preloadingStrategy: PreloadAllModules})
        ],
        exports: [RouterModule]
        })
        export class AppRoutingModule {
        }
    

    AuthModule

    @NgModule({
      declarations: [
        SigninFormComponent,
        SignupRequestFormComponent,
      ],
      imports: [
        CommonModule,
        FormsModule,
        BrowserModule,
        BrowserAnimationsModule,
        MDBBootstrapModule,
        MDBBootstrapModulePro,
        NgbModule,
        AuthRoutingModule
      ]
    })
    export class AuthModule { }
    

    AuthRoutingModule

    const authRoutes: Routes = [
     { path: 'signin', component: SigninFormComponent },
     { path: 'signup', component: SignupRequestFormComponent }
    ];
    
    @NgModule({
      imports: [
        RouterModule.forChild(authRoutes)
      ],
      exports: [RouterModule]
    })
    export class AuthRoutingModule { }
    

    应用程序模块

    @NgModule({
      declarations: [
        AppComponent,
        ErrorPageComponent,
        NotFoundComponent,
      ],
      imports: [
        BrowserModule,
        BrowserAnimationsModule,
        FormsModule,
        HttpModule,
        CoreModule,
        AuthModule,
        AppRoutingModule
      ],
      providers: [
        MDBSpinningPreloader,
        UserService,
        ConfigService,
        AuthGuard,
        { provide: Http, useClass: AuthenticatedHttpService }
      ],
      bootstrap: [AppComponent],
    })
    export class AppModule { }
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   test_124 Jim Flood    7 年前

    应用程序模块

     @NgModule({
        .....
        imports: [
         BrowserModule,
         BrowserAnimationsModule,
         FormsModule,
         HttpModule,
         CoreModule,
         AuthModule,
         AppRoutingModule
        ],
    

    您首先加载CoreModule,因此首先加载AppRoutingModule,每个无效路由和未定义的路由都被重定向到widlcard表达式。

        const appRoutes: Routes = [
            { path: '', redirectTo: 'home' , pathMatch: 'full' },
            { path: 'home', component: HomeComponent },
            { path: 'not-found', component: NotFoundComponent, data: { message: 'We Could Not Serve Your Request!'}},
            { path: '**', redirectTo: '/not-found',  pathMatch: 'full'}
        ];
    

    所以,要么在AppModule声明中在CoreModule之前加载AuthModule,要么从AppRoutingModule中删除通配符表达式并将其放入AuthRoutingModule中。