Angular 为什么组件即使不在任何模块的声明中也可以访问?
我有Angular 为什么组件即使不在任何模块的声明中也可以访问?,angular,typescript,single-page-application,web-frontend,ng-modules,Angular,Typescript,Single Page Application,Web Frontend,Ng Modules,我有PageNotFoundComponent,我没有在任何模块中声明它。然而,我已经将其包含在路由模块中,以确保当我们没有匹配的路由时,该组件将位于应用程序组件的路由器出口中 app.component.html <app-header></app-header> <br> <div class="container"> <router-outlet></router
PageNotFoundComponent
,我没有在任何模块中声明它。然而,我已经将其包含在路由模块中,以确保当我们没有匹配的路由时,该组件将位于应用程序组件的路由器出口中
app.component.html
<app-header></app-header>
<br>
<div class="container">
<router-outlet></router-outlet>
</div>
应用程序路由模块
import { Routes, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { AuthComponent } from './auth/auth.component';
const appRoutes: Routes = [
{ path: '', redirectTo: '/recipes', pathMatch: 'full' },
{ path: 'shoppinglist', component: ShoppingListComponent },
{ path: 'auth', component: AuthComponent },
{
path: 'not-found', component: PageNotFoundComponent, data: { message: '404 error ! Explore our site to know more' }
},
{ path: '**', redirectTo: '/not-found' }
]
@NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
现在,当我转到某个不可用的路由时,我会自动转到“未找到页面”组件并成功加载
注意:使用Angular 9版本…可能有人会知道是否添加了任何此类功能。如果此组件既没有在
声明中声明
也没有通过entryComponents声明
,则此操作不起作用。我想这是一个缓存/热重加载问题。尝试重新编译并再次测试它。如果仍然出现这种情况,请提供一个工作堆栈Blizz,以再现如果您使用--prod
标志进行构建会发生什么?与@enno.void和David的注释相同,这可能是JIT与AOT的较量issue@ChrisYungmann我处于使用aot编译的开发模式中,如果此组件既没有在声明中声明,也没有通过entryComponents声明,则此组件不应工作。我想这是一个缓存/热重加载问题。尝试重新编译并再次测试它。如果仍然出现这种情况,请提供一个工作堆栈Blizz来复制如果您使用--prod
标志进行构建会发生什么?与@enno.void和David的评论相同,这可能是JIT vs AOTissue@ChrisYungmann我处于使用aot编译的开发模式
import { Routes, RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { AuthComponent } from './auth/auth.component';
const appRoutes: Routes = [
{ path: '', redirectTo: '/recipes', pathMatch: 'full' },
{ path: 'shoppinglist', component: ShoppingListComponent },
{ path: 'auth', component: AuthComponent },
{
path: 'not-found', component: PageNotFoundComponent, data: { message: '404 error ! Explore our site to know more' }
},
{ path: '**', redirectTo: '/not-found' }
]
@NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}