gpt4 book ai didi

angular - 找不到要加载的主 socket 'LocalizationListComponent'

转载 作者:行者123 更新时间:2023-12-02 12:01:18 25 4
gpt4 key购买 nike

我正在创建一个 Angular 2 RC5 应用程序,每个模块都是延迟加载的。当应用程序启动时,它会显示 LocalizationListComponent 中的列表。 ,如预期,但控制台中有一条消息,显示 Cannot find primary outlet to load 'LocalizationListComponent' 。考虑到列表已显示并且错误提示该组件非常奇怪和不寻常。

为了解决这个问题,我需要添加 <router-outlet>ROUTER_DIRECTIVES 一起受此错误影响的每个组件和 View 。这样做的问题是该列表将显示两次,并且详细信息屏幕会加载到该列表下。这不是我想要的行为。

在此之后,我浏览了 angular.io 上的 Angular 2 路由教程,并检查了他们的 plunker 代码。一切似乎都相同,但错误仍然存​​在。

我已将应用程序上传到保管箱 ( inactive link )

以下是模块、路由和组件(很抱歉列表太长):

app.routes.ts

import { Routes, RouterModule } from "@angular/router";

const routes: Routes = [
{
path: "",
redirectTo: "/localizations",
pathMatch: "full"
},
{
path: "localizations",
loadChildren: "./app/modules/localization/localization.module",
}
];

export const appRoutes = RouterModule.forRoot(routes);

app.module.ts

import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { appRoutes } from "../routes/app.routes"

import { AppComponent } from "../components/app.component";
import { AppNavigationComponent } from "../components/app-navigation.component";
import { AppStartComponent } from "../components/app-start.component";

import { LoggerModule } from "./logging/logger.module";
import { SharedModule } from "./shared/shared.module";

@NgModule({
imports: [ BrowserModule, LoggerModule.forRoot(), SharedModule, appRoutes ],
declarations: [ AppComponent, AppNavigationComponent, AppStartComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule {

}

app.component.ts

/// <reference path="../../typings/globals/node/index.d.ts" />

import { Component, OnInit, OnDestroy } from "@angular/core";
import { ActivatedRoute, Router, ROUTER_DIRECTIVES } from "@angular/router";
import { AppNavigationComponent } from "./app-navigation.component";

import { LoggerMessageType } from "../modules/logging/models/enums/LoggerMessageType";
import { AbstractLogger } from "../modules/logging/interfaces/AbstractLogger";

@Component({
moduleId: module.id,
selector: "vms-localization-app",
templateUrl: "../views/app.component.html",
directives: [ ROUTER_DIRECTIVES ]
})
export class AppComponent {
status: string;

constructor(private logger: AbstractLogger) {
this.status = "Alpha 0.1";
}

ngOnInit() : any {

}

ngOnDestroy() : any {

}
}

本地化.module.ts

import { NgModule } from "@angular/core";
import { RouterModule } from "@angular/router";
import { CommonModule } from "@angular/common";
import { FormsModule } from "@angular/forms";
import { localizationRoutes } from "./routes/localization.routes";

import { LocalizationListComponent } from "./components/localization-list.component";
import { LocalizationDetailsComponent } from "./components/localization-details.component";

import { AbstractLocalizationService } from "./interfaces/AbstractLocalizationService";
import { MockLocalizationService } from "./services/mock-localization.service";

@NgModule({
imports: [ localizationRoutes, CommonModule, FormsModule, RouterModule ],
declarations: [ LocalizationListComponent, LocalizationDetailsComponent ],
providers: [ { provide: AbstractLocalizationService, useClass: MockLocalizationService } ]
})
export default class LocalizationModule {

}

本地化.routes.ts

import { Routes, RouterModule } from "@angular/router";
import { LocalizationListComponent } from "../components/localization-list.component";
import { LocalizationDetailsComponent } from "../components/localization-details.component";

const routes: Routes = [
{
path: "",
component: LocalizationListComponent,
children: [
{
path: "localization",
component: LocalizationListComponent
},
{
path: ":id",
component: LocalizationDetailsComponent
},
{
path: "",
component: LocalizationListComponent
}
]
}
];

export const localizationRoutes = RouterModule.forChild(routes);

本地化列表.component.ts

import { Component, OnInit } from "@angular/core";
import { Localization } from '../models/localization';
import { Language } from "../models/language";

import { AbstractLocalizationService } from "../interfaces/AbstractLocalizationService";
import { AbstractLogger } from "../../logging/interfaces/AbstractLogger";
import { LoggerMessageType } from "../../logging/models/enums/LoggerMessageType";

@Component({
moduleId: module.id,
selector: "localization-list",
templateUrl: "../views/localization-list.component.html"
})
export class LocalizationListComponent implements OnInit {
localizations: Localization[];
languages: Language[];

constructor(private localizationService: AbstractLocalizationService,
private logger: AbstractLogger) {

}

ngOnInit() : any {
this.localizationService.getLocalizations()
.subscribe((result: Localization[]) => {
this.localizations = result;
this.languages = this.localizations[0].languages;
});
}
}

app.component.html

<div class="container">
<div class="page-header">
<h1>VMS Localization<small>{{ status }}</small></h1>
<app-navigation></app-navigation>
</div>

<router-outlet></router-outlet>
</div>

上面的片段(以及提供的项目)一定有问题,但我看不到任何问题。

我还检查了 StackOverflow 上的其他问题,但它们主要涉及 app.component.ts 中缺少的 ROUTER_DIRECTIVES

感谢您的帮助!

最佳答案

我刚刚在 GitHub ( https://github.com/angular/angular/issues/10686 ) 上遇到一个问题,其中解释说这是一种故意行为。每一位拥有 children 的家长(似乎 loadChildren 也是如此)必须有自己的 socket 来加载子项。

似乎这在 Router documentation 中没有任何记录。 .

更新:当您声明children时在模块的路由器中,每个父级都必须有自己的 <router-outlet></router-outlet>为了加载这个 child 。如何实现这个取决于需求。对于我的解决方案,我创建了一个空组件,其中仅包含 <router-outlet></router-outlet>并定义路线如下:

import { Routes, RouterModule } from "@angular/router"
import { SomeParentComponent } from "./app/modules/SomeModule/SomeParentComponent"
import { SomeChildComponent1 } from "./app/modules/SomeModule/SomeChild1Component"
import { SomeChildComponent2 } from "./app/modules/SomeModule/SomeChild2Component"

const routes: Routes = [
path: "",
component: SomeParentComponent,
children: [
{
path: "",
component: SomeChild1Component
},
{
path: ":id",
component: SomeChild2Component
}
]
]

export const someModuleRoutes = RouterModule.forChild(routes);

这应该可以解决问题,因为主组件路由将调用 loadChildren指着SomeParentComponent .

关于angular - 找不到要加载的主 socket 'LocalizationListComponent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943629/

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