没有为ChildrenOutletContexts提供程序(injectionError) [英] No provider for ChildrenOutletContexts (injectionError)

查看:119
本文介绍了没有为ChildrenOutletContexts提供程序(injectionError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Angular CLI为我的应用程序生成路由模块时出现以下错误:

I am getting the following error when using Angular CLI to generate a routing module for my application:

ERROR Error: No provider for ChildrenOutletContexts!
    at injectionError (core.es5.js:1169)
    at noProviderError (core.es5.js:1207)
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._throwOrNull (core.es5.js:2649)
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._getByKeyDefault (core.es5.js:2688)
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_._getByKey (core.es5.js:2620)
    at ReflectiveInjector_.webpackJsonp.../../../core/@angular/core.es5.js.ReflectiveInjector_.get (core.es5.js:2489)
    at resolveNgModuleDep (core.es5.js:9481)
    at NgModuleRef_.webpackJsonp.../../../core/@angular/core.es5.js.NgModuleRef_.get (core.es5.js:10569)
    at resolveDep (core.es5.js:11072)
    at createClass (core.es5.js:10936)

我使用Angular CLI生成了路由模块,如下所示:

I generated the routing module with Angular CLI like this:

ng generate module --routing App

这是路由模块的内容:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MyComponent } from './my/my.component';

const routes: Routes = [
  {
    path: '',
    component: MyComponent,
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
  declarations: []
})
export class AppRoutingModule { }

这就是我的app.component.html中的内容:

<div class="container" role="main">
    <router-outlet></router-outlet>
</div>

此错误的原因可能是什么?

What can be the reason of this error?

推荐答案

要解决此问题,请更改此行:

To solve this problem change this line:

imports: [RouterModule.forChild(routes)],

收件人:

imports: [RouterModule.forRoot(routes)],

由于RouterModule.forChild()生成的模块包含必要的指令和路由,但不包含路由服务,导致了该错误.这就是RouterModule.forRoot的目的:它生成一个模块,其中包含必要的指令,路由,路由服务.

The error is caused because RouterModule.forChild() generates a module that contains the necessary directives and routes but doesn't include the routing service. That is what RouterModule.forRoot is for: it generates a module that contains the necessary directives, routes, and the routing service.

Angular文档中的更多信息: Angular-路由器模块.

More information in Angular docs: Angular - Router Module.

这篇关于没有为ChildrenOutletContexts提供程序(injectionError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆