【问题标题】:Angular2 - 'router-outlet' is not a known elementAngular2 - “路由器插座”不是已知元素
【发布时间】:2017-03-11 07:40:57
【问题描述】:

我创建了具有深度子路径的路线。我将<router-outlet> 添加到包装到 NgModule 中的 AdminComponent 组件中。但是刷新页面后我得到了这个错误:

'router-outlet' is not a known element

可能是因为我忘记将某些模块导入到 admin.module.ts

请帮忙。谢谢。

app.routes.ts

export const routes: Routes = [
    {
        path: '',
        component: AppComponent,
        children: [
            {
                path: '',
                component: LoginComponent
            },
            {
                path: 'admin',
                component: AdminComponent
            },
            {
                path: 'user',
                component: UserComponent
            },
            {
                path: 'there',
                component: ThereComponent
            }
        ]
    }
]

app.module.ts

@NgModule({
    imports: [
        BrowserModule,
        AppRoutes,
        FormsModule,
        ReactiveFormsModule,
        HttpModule,
        RouterModule,
        TranslateModule.forRoot({
            provide: TranslateLoader,
            useFactory: (http: Http) => {
                return new TranslateStaticLoader(http, './src/assets/i18n', '.json')
            },
            deps: [Http]
        }),
        UserComponentModule,
        AdminComponentModule,
        LoginComponentModule,
        ThereComponentModule,
        DashboardComponentModule
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        FormBuilder
    ],
    bootstrap: [AppComponent]
})

admin.component.tsadmin.module.ts

//  admin.component.ts
import {Component} from "@angular/core";

@Component({
    selector: 'admin',
    template: "<router-outlet></router-outlet>",
})

export class AdminComponent {
    constructor() {

    }
}

//  admin.module.ts
const ADMIN_DECLARATION = [
    AdminComponent
];

@NgModule({
    imports: [
        BrowserModule,
        TranslateModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        ADMIN_DECLARATION
    ],
    exports: [
        ADMIN_DECLARATION
    ],
    providers: [
        TranslateService,
        FormBuilder
    ]
})

export class AdminComponentModule {

}

【问题讨论】:

  • 我只是错过了;

标签: angular angular2-routing


【解决方案1】:

AdminComponentAdminComponentModule 的一部分,您还没有在AdminComponentModule 模块中导入RouterModule

//  admin.component.ts
import {Component} from "@angular/core";

@Component({
    selector: 'admin',
    template: "<router-outlet></router-outlet>",
})

export class AdminComponent {
    constructor() {

    }
}

//  admin.module.ts
const ADMIN_DECLARATION = [
    AdminComponent
];

@NgModule({
    imports: [
        BrowserModule,
        TranslateModule,
        RouterModule,
        FormsModule,
        ReactiveFormsModule
    ],
    declarations: [
        ADMIN_DECLARATION
    ],
    exports: [
        ADMIN_DECLARATION
    ],
    providers: [
        TranslateService,
        FormBuilder
    ]
})

export class AdminComponentModule {

}

【讨论】:

  • 嗨@yankee 我正在使用惰性模块功能,并且我已经分离了 app.routing.module.ts 文件。如果我包含 'import { AppRoutingModule } from './app-routing.module';并导入 app.module.ts 然后我再次面临stackoverflow.com/questions/49670232/… 问题。如果可能,请您提供帮助'
【解决方案2】:

添加此代码

import { provideRoutes} from '@angular/router';

给你的app.module.ts

为我工作。

【讨论】:

  • 我的荣幸先生 :)
  • 谢谢它帮助了我......一般问你们是从网站上阅读所有文档还是只是通过经验了解事情???
  • @Akhil 我很高兴不,我只是像你一样抬头,一些评论帮助了我。仅仅阅读文档对我来说从来都不是一件容易的事!
【解决方案3】:

您没有导出 RouterModule。

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

【讨论】:

  • 是的,先生。谢谢你!
【解决方案4】:

在你的 app.module.ts 文件中

import { routing } from './app-routing.module';

//and then write within imports
@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent
  ],
  imports: [
    BrowserModule,
    **routing**,
    EmployeeModule
  ],

【讨论】:

    【解决方案5】:

    这对我有用:

    在 AppModule 中添加架构 [NO_ERRORS_SCHEMA]

    import { NO_ERRORS_SCHEMA } from '@angular/core';
    
    @NgModule({
      schemas : [NO_ERRORS_SCHEMA]  
    })
    

    【讨论】:

      【解决方案6】:

      app.module.ts

      import { MyRoutingModule } from './MyRoutingModulePath';
      
        @NgModule({
        imports: [
          MyRoutingModule
        ])
      

      【讨论】:

      • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和文档链接。如果没有围绕它的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案。
      • 我迟来的回答中没有任何原创内容。我努力实施通过互联网找到的解决方案。由于我的架构特殊性,存在、令人困惑和耗时。我一直在寻找一个简短的答案来指出我的解决方案,而不是教我这个过程。在我终于做到了之后,对我来说,在完整的例子中很难观察到什么变得很明显。当然,失败的方式有很多。但是很多人会像我一样失败。这个答案适用于那些非常接近修复它但距离正确的人。
      猜你喜欢
      • 1970-01-01
      • 2017-07-26
      • 2020-06-08
      • 2021-11-11
      • 2017-04-29
      • 1970-01-01
      • 2017-05-07
      • 2017-03-21
      • 2021-03-13
      相关资源
      最近更新 更多