gpt4 book ai didi

javascript - Angular 2 子应用、父/子、嵌套组件、嵌套路由器 socket 、设计/编码

转载 作者:可可西里 更新时间:2023-11-01 02:06:10 24 4
gpt4 key购买 nike

我有一个 Angular 2 应用程序。主屏幕(应用程序?)看起来像这样......

Main app screen

当您单击顶部菜单 routerLinks 中的项目时,新组件将加载到主视图路由器 socket 中。其中一个链接加载了一个新的“管理”模块/组件,它有自己的路由和新的路由器导出......

Admin app screen

然后当您单击左侧导航栏中的 routerLinks 时,新的管理组件将加载到新的路由器 socket 中。

但是……

Angular 2 不允许超过 1 个路由器 socket 。因此,单击左侧导航栏中的任何 routerLink 只会替换整个初始路由器导出 View 。

我看过一些关于使用“bootstrap”加载后续组件的 SO 帖子(较旧的,可能已弃用),但我无法让它工作。我什至无法从'任何地方导入{ bootstrap },没有任何作用'。所以也许这不是执行此操作的方法。

如何让 Admin 子应用部分正常工作?

非常非常感谢您分享您的 Angular 2 专业知识:-)

编辑:尝试下面建议的解决方案。无论我将路由放在哪里,在基础 app.routes.ts 或子应用 admin.routes.ts 中,无论我如何格式化routerLinks,我不断收到此错误...

Cannot find route

再次编辑:这是路由器和模板中的代码...

<!--
============================================================================
/src/app/component/admin/admin.component.html
-->

<!-- Row for entire page columnar dispaly -->
<div class="row">

<!-- Column 1: Left navigation, links to all admin components -->
<div class="col col-md-4">
<app-admin-nav></app-admin-nav>
</div>

<!-- Column 2: Rows of records, click to edit -->
<div class="col col-md-8">
<router-outlet name="admin-app"></router-outlet>
</div>

</div>

// ============================================================================
// /src/app/app.routes.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AppComponent } from './app.component';
import { GameComponent } from './component/game/game.component';
import { HomeComponent } from './component/home/home.component';
import { LoginComponent } from './component/login/login.component';
import { PlayerComponent } from './component/player/player.component';
import { AuthGuard } from './service/auth/auth.service';
import { SignupComponent } from './component/signup/signup.component';
import { EmailComponent } from './component/email/email.component';
import { AdminComponent } from './component/admin/admin.component';
// import { AdminWorldComponent } from './component/admin/world/admin-world.component';
// import { AdminModuleComponent } from './component/admin/module/admin-module.component';
// import { AdminRegionComponent } from './component/admin/region/admin-region.component';

export const router: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' }
, { path: 'home', component: HomeComponent }
, { path: 'game', component: GameComponent, canActivate: [AuthGuard] }
, { path: 'admin', component: AdminComponent, canActivate: [AuthGuard] }
// , {
// path: 'admin', component: AdminComponent, canActivate: [AuthGuard],
// children: [
// { path: 'world', component: AdminWorldComponent, outlet: 'admin-app' },
// { path: 'module', component: AdminModuleComponent, outlet: 'admin-app' },
// { path: 'region', component: AdminRegionComponent, outlet: 'admin-app' }
// ]
// },
, { path: 'login', component: LoginComponent }
, { path: 'signup', component: SignupComponent }
, { path: 'login-email', component: EmailComponent }
, { path: 'players', component: PlayerComponent, canActivate: [AuthGuard] }
];

export const routes: ModuleWithProviders = RouterModule.forRoot(router);

// ============================================================================
// /src/app/component/admin/admin.routes.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AdminComponent } from './admin.component';
import { AdminWorldComponent } from './world/admin-world.component';
import { AdminModuleComponent } from './module/admin-module.component';
import { AdminRegionComponent } from './region/admin-region.component';

export const router: Routes = [
{
path: 'admin', component: AdminComponent,
children: [
{ path: 'world', component: AdminWorldComponent, outlet: 'admin-app' }
, { path: 'module', component: AdminModuleComponent, outlet: 'admin-app' }
, { path: 'region', component: AdminRegionComponent, outlet: 'admin-app' }
]
}
];

export const routes: ModuleWithProviders = RouterModule.forRoot(router);

编辑 3: 尝试将 RouterModule.forRoot 更改为 RouterModule.forChild,遗憾的是,同样的错误:-/

编辑 4: 将路由转换为使用 2 个路由模块。希望这可能会有所作为,但同样的错误。

新路由器...

// ============================================================================
// /src/app/app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component';
import { GameComponent } from './component/game/game.component';
import { HomeComponent } from './component/home/home.component';
import { LoginComponent } from './component/login/login.component';
import { PlayerComponent } from './component/player/player.component';
import { AuthGuard } from './service/auth/auth.service';
import { SignupComponent } from './component/signup/signup.component';
import { EmailComponent } from './component/email/email.component';
import { AdminComponent } from './component/admin/admin.component';

export const appRoutes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' }
, { path: 'home', component: HomeComponent }
, { path: 'game', component: GameComponent, canActivate: [AuthGuard] }
, { path: 'admin', component: AdminComponent, canActivate: [AuthGuard] }
// , {
// path: 'admin', component: AdminComponent, canActivate: [AuthGuard],
// children: [
// { path: 'world', component: AdminWorldComponent, outlet: 'admin-app' },
// { path: 'module', component: AdminModuleComponent, outlet: 'admin-app' },
// { path: 'region', component: AdminRegionComponent, outlet: 'admin-app' }
// ]
// },
, { path: 'login', component: LoginComponent }
, { path: 'signup', component: SignupComponent }
, { path: 'login-email', component: EmailComponent }
, { path: 'players', component: PlayerComponent, canActivate: [AuthGuard] }
];

@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
]
})
export class AppRoutingModule { }

// ============================================================================
// /src/app/admin/admin-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AdminComponent } from './admin.component';
import { AdminWorldComponent } from './world/admin-world.component';
import { AdminModuleComponent } from './module/admin-module.component';
import { AdminRegionComponent } from './region/admin-region.component';

export const adminRoutes: Routes = [
{
path: 'admin', component: AdminComponent,
children: [
{ path: 'world', component: AdminWorldComponent, outlet: 'admin-app' }
, { path: 'module', component: AdminModuleComponent, outlet: 'admin-app' }
, { path: 'region', component: AdminRegionComponent, outlet: 'admin-app' }
]
}
];

@NgModule({
imports: [
RouterModule.forChild(adminRoutes)
],
exports: [
RouterModule
]
})
export class AdminRoutingModule { }

编辑 5:它正在运行!

删除了路由模块,根据 Tyler 的建议返回到导出路由配置。他是对的,路由模块不起作用。泰勒和我一起工作了很多,所以我接受他的回答。谢谢泰勒的帮助!

这里介绍了如何使用自己的路由器 socket 设置父应用程序,然后在父应用程序上单击一个链接以加载具有自己的新路由器 socket 的子应用程序。子应用加载/替换父应用路由器导出。

父应用模块或路由中确实没有什么特别之处。它们就是我在这篇文章之前拥有它们的方式。

需要注意的要点,至少在我今天的情况下,不要在子路由器中使用 name="" 属性-导出。这将导致“错误:无法匹配任何路由...”。 不要像我在上面尝试的那样使用路由模块,这也会导致“错误:无法匹配任何路由...”。 不要在路由中使用outlet: 'blah',这也会导致“错误:无法匹配任何路由...”。确保完全按照下面在 admin.routes.ts 中看到的方式设置子路由配置 children: []。另外,请注意子路由中的 RouterModule.forChild(router)。这些东西今天为我解决了这个问题。

家长应用

// ============================================================================
// src/app/app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AngularFireModule } from 'angularfire2';
import { firebaseConfig } from '../environments/firebase.config';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
// import { AppRoutingModule } from './app-routing.module';
import { routes } from './app.routes';

// Components
import { AppComponent } from './app.component';
import { HomeComponent } from './component/home/home.component';
import { GameComponent } from './component/game/game.component';
import { PlayerComponent } from './component/player/player.component';
import { LoginComponent } from './component/login/login.component';
import { SignupComponent } from './component/signup/signup.component';
import { EmailComponent } from './component/email/email.component';

// Admin Module
import { AdminModule } from './component/admin/admin.module';

// Services
import { AuthGuard } from './service/auth/auth.service';
import { AuthPlayerService } from './service/auth/auth-player.service';
import { MdbService } from './service/mongo/mdb.service';
import { PlayerMdbService } from './service/mongo/player-mdb.service';

@NgModule({
declarations: [
AppComponent
, HomeComponent
, GameComponent
, PlayerComponent
, LoginComponent
, SignupComponent
, EmailComponent
],
imports: [
BrowserModule
, FormsModule
, HttpModule
, AdminModule
, AngularFireModule.initializeApp(firebaseConfig)
, NgbModule.forRoot()
// , AppRoutingModule
, routes
],
providers: [
AuthGuard
, AuthPlayerService
, MdbService
, PlayerMdbService
],
bootstrap: [AppComponent]
})
export class AppModule { }

// ============================================================================
// /src/app/app.routes.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { GameComponent } from './component/game/game.component';
import { HomeComponent } from './component/home/home.component';
import { LoginComponent } from './component/login/login.component';
import { PlayerComponent } from './component/player/player.component';
import { AuthGuard } from './service/auth/auth.service';
import { SignupComponent } from './component/signup/signup.component';
import { EmailComponent } from './component/email/email.component';
import { AdminComponent } from './component/admin/admin.component';

export const router: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'game', component: GameComponent, canActivate: [AuthGuard] },
{ path: 'admin', component: AdminComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: 'signup', component: SignupComponent },
{ path: 'login-email', component: EmailComponent },
{ path: 'players', component: PlayerComponent, canActivate: [AuthGuard] }
];

export const routes: ModuleWithProviders = RouterModule.forRoot(router);

子应用

// ============================================================================
// /src/app/admin/admin.module.ts

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { routes } from './admin.routes';
// import { AdminRoutingModule } from './admin-routing.module';
import { AdminComponent } from './admin.component';
import { AdminRecsComponent } from './admin-recs.component';
import { AdminFormComponent } from './admin-form.component';
import { AdminNavComponent } from './admin-nav.component';

import { AdminWorldComponent } from './world/admin-world.component';
import { AdminModuleComponent } from './module/admin-module.component';
import { AdminRegionComponent } from './region/admin-region.component';

@NgModule({
imports: [
CommonModule
, FormsModule
// , AdminRoutingModule
, routes
]
, declarations: [
AdminComponent
, AdminNavComponent
, AdminRecsComponent
, AdminFormComponent
, AdminWorldComponent
, AdminModuleComponent
, AdminRegionComponent
]
, schemas: [CUSTOM_ELEMENTS_SCHEMA]
, exports: [
AdminRecsComponent
, AdminFormComponent
, AdminNavComponent
// , AdminWorldComponent
// , AdminModuleComponent
// , AdminRegionComponent
]
// , bootstrap: [AdminComponent]
})
export class AdminModule { }


// ============================================================================
// /scr/app/admin/admin.routes.ts

import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AdminComponent } from './admin.component';
import { AdminWorldComponent } from './world/admin-world.component';
import { AdminModuleComponent } from './module/admin-module.component';
import { AdminRegionComponent } from './region/admin-region.component';

export const router: Routes = [
{
path: 'admin', component: AdminComponent,
children: [
{ path: 'world', component: AdminWorldComponent },
{ path: 'module', component: AdminModuleComponent },
{ path: 'region', component: AdminRegionComponent },
]
}
];

export const routes: ModuleWithProviders = RouterModule.forChild(router);

最佳答案

不确定您在哪里听说 Angular2 不允许超过 1 个 router-outlet。我在大型应用程序中使用了多个。

您的主要 app.component 将有一个 router-outlet 来处理根路由。如果您的其中一条路由延迟加载了管理模块,则该管理模块将拥有包含侧边菜单栏和所有子路由的 router-outlet 的根组件。

例子:

//app.routes

export const ROUTES: Routes = [
// Main redirect
{ path: '', component: MainViewComponent },

{
path: 'admin',
loadChildren: './admin/admin.module#AdminModule'
}
]

您的 MainViewComponent 可以包含顶部导航栏和一个 router-outlet

然后 Admin 路由器配置可能如下所示:

export const routes: Routes = [
{
path: '',
component: AdminComponent,
children: [
{ path: '', component: Component1},
{ path: 'component2', component: Component2}

]
}
];

Admin 模块中的根组件可能包含侧边栏菜单和一个router-outlet 以显示子组件。

你也可以命名为 router-outlets。一个例子是并排放置两个 router-outlet:

<router-outlet></router-outlet>
<router-outlet name="popup"></router-outlet>

您的路由器配置如下所示:

{
path: 'compose',
component: ComposeMessageComponent,
outlet: 'popup'
},

你会像这样使用它:

<a [routerLink]="[{ outlets: { popup: ['compose'] } }]">Contact</a>

或者用这个清除内容:

this.router.navigate([{ outlets: { popup: null }}]);

参见 docs或者这个 article了解更多详情。

希望对您有所帮助。

编辑

当为延迟加载的 child 使用路由配置时,请确保您的路由配置已正确加载到您的模块中。根路由配置将通过 RouterModule.forRoot(routes) 加载到根 AppModule 中,子路由通过 RouterModule.forChild(routes) 加载到子模块中。

你的路由配置和模块需要看起来像这样(不要创建一个单独的模块来保存路由配置):

//管理路由

export const adminRoutes: Routes = [
{
path: 'admin', component: AdminComponent,
children: [
{ path: 'world', component: AdminWorldComponent, outlet: 'admin-app' }
, { path: 'module', component: AdminModuleComponent, outlet: 'admin-app' }
, { path: 'region', component: AdminRegionComponent, outlet: 'admin-app' }
]
}
];

//管理模块:

import { adminRoutes } from './admin.routes';

@NgModule({
imports: [
...
RouterModule.forChild(adminRoutes),
]
...

//App Routes(延迟加载Admin模块)

export const appRoutes: Routes = [
{ path: 'admin', loadChildren: './admin/admin.module#AdminModule' },
....

//应用模块

import { appRoutes } from './app.routes';

@NgModule({
imports: [
...
RouterModule.forRoot(appRoutes),
]
...

希望对您有所帮助。

关于javascript - Angular 2 子应用、父/子、嵌套组件、嵌套路由器 socket 、设计/编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com