[英]AngularJS: Inject service or directives dynamically in controller (without including them in angular.module's square brackets beforehand, see example)
[英]Angular 5: I can navigate to a module's route without including the module's path segment in the URL?
因此,我構建了一個Angular 5.2.0應用程序,該應用程序分為3個模塊:
admin
模塊,在用戶導航到任何admin/**
路由時加載, prefect
模塊,當用戶導航至任何prefect/**
路線時將加載, authentication
模塊,在所有其他情況下都將加載。 每個模塊都有自己的路由。 例如, /admin/student/list
激活在admin
模塊中定義的StudentListComponent
。
現在,如果我轉到/student/list
,而沒有/admin
部分,它將加載相同的StudentListComponent
! 並且可以使用應用程序模塊中定義的每條路線進行復制。
這顯然不是可取的行為。 特別是考慮到我已經設置了路由保護程序來保護admin
和prefect
模塊,這使得繞開它們非常容易。
任何見解表示贊賞。 是蟲子嗎? 還是我做錯了什么?
這是我在app.module.ts
的代碼:
export const routes: Routes = [
{
path: 'admin',
loadChildren: 'app/modules/admin/admin.module#AdminModule',
canActivate: [AdminGuard]
},
{
path: 'prefect',
loadChildren: 'app/modules/prefect/prefect.module#PrefectModule',
canActivate: [PrefectGuard]
},
{
path: '',
loadChildren:
'app/modules/authentication/authentication.module#AuthenticationModule'
},
{
path: '**',
redirectTo: '/login'
}
];
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
RouterModule.forRoot(routes),
AuthenticationModule,
PrefectModule,
AdminModule
],
providers: [AdminGuard, PrefectGuard],
bootstrap: [AppComponent]
})
export class AppModule {}
並在admin.module.ts
:
const routes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{
path: 'student',
children: [
{
path: 'list',
component: StudentListComponent
},
{
path: 'new',
component: StudentEditComponent
},
{
path: ':id',
component: StudentItemComponent
},
{
path: ':id/edit',
component: StudentEditComponent
}
]
}
]
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes),
HttpClientModule,
FormsModule,
// Angular Material modules...
NavigationModule
],
providers: [StudentService],
declarations: [
AdminComponent,
StudentListComponent,
StudentItemComponent,
StudentEditComponent
]
})
export class AdminModule {}
並在authentication.module.ts
:
const routes: Routes = [
{
path: '',
component: AuthenticationComponent,
children: [
{
path: 'login',
component: LoginComponent
},
{
path: 'reset',
component: ResetPasswordComponent
},
{
path: '',
redirectTo: '/login',
pathMatch: 'full'
}
]
}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes),
FormsModule,
// Angular Material modules...
],
providers: [AuthenticationService, AdminService, StudentService],
declarations: [
AuthenticationComponent,
LoginComponent,
ResetPasswordComponent
]
})
export class AuthenticationModule {}
您將在這里將延遲加載的AdminModule導入應用程序組件中:
imports: [
BrowserModule,
BrowserAnimationsModule,
RouterModule.forRoot(routes),
AuthenticationModule,
PrefectModule,
AdminModule
],
因此實際上不是延遲加載,而是在AppModule加載時加載。 因此,以后可以從'/ admin'和根目錄'/'訪問AdminModule的路由。 只需將其從AppModule的導入中刪除,就可以了。
我創建了一個小型回購,其中提供了您要在此處實現的目標的工作示例
希望能有所幫助
好的,我找到了解決方案:在app.module.ts
,我正在導入功能模塊( AdminModule
等),並且以某種方式產生了這種效果。
我刪除了這些導入,並解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.