typescript - 从共享模块注入(inject)组件时,Angular 5 静态注入(inject)器错误

标签 typescript angular5 angular-components ng-modules

这是我的组件文件。我正在尝试使用 ng-bootstraps 模态创建一个可重用的模态组件。当我尝试从共享模块导入以下组件时,出现静态注入(inject)器错误。我查看了许多其他资源,但无法弄清楚我的实现有什么问题。

   import {Component} from '@angular/core';

    import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

    @Component({
      selector: 'ngbd-modal-basic',
      templateUrl: './customM.component.html'
    })
    export class CustomModalComponent {
      closeResult: string;

      constructor(private modalService: NgbModal) {}

      open(content) {
        this.modalService.open(content).result.then((result) => {
          this.closeResult = `Closed with: ${result}`;
        }, (reason) => {
          this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        });
      }

      private getDismissReason(reason: any): string {
        if (reason === ModalDismissReasons.ESC) {
          return 'by pressing ESC';
        } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
          return 'by clicking on a backdrop';
        } else {
          return  `with: ${reason}`;
        }
      }
    }

下面是声明并导出 ModalComponent 的 SharedModule。

import { NgModule  } from '@angular/core';
import { CommonModule } from '@angular/common';

import { LoaderComponent } from './loader/loader.component';

import * as Shared from './index'; 

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    LoaderComponent,
    Shared.CustomModalComponent
  ],
  exports: [
    LoaderComponent,
    Shared.CustomModalComponent
  ],
  providers: []
})
export class SharedModule { 
  // static forRoot() : ModuleWithProviders {
  //   return {
  //     ngModule: SharedModule,
  //     providers: [ WebsocketService ]
  //   }
  // }
}

一旦在构造函数中声明静态注入(inject)器错误,就会在这里抛出该错误。

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { finalize } from 'rxjs/operators';

import { environment } from '@env/environment';
import { Logger, I18nService, AuthenticationService } from '@app/core';

import { CustomModalComponent } from '../shared/_directives/custom-modal/customM.component';

const log = new Logger('Login');

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {

  version: string = environment.version;
  error: string;
  loginForm: FormGroup;
  isLoading = false;

  constructor(private router: Router,
              private formBuilder: FormBuilder,
              private i18nService: I18nService,
              private authenticationService: AuthenticationService,
              private c: CustomModalComponent) {
    this.createForm();
  }

主App.module文件

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule, NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { TranslateModule } from '@ngx-translate/core';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { CoreModule } from '@app/core';
import { SharedModule } from '@app/shared';
import { HomeModule } from './home/home.module';
import { AboutModule } from './about/about.module';
import { LoginModule } from './login/login.module';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    NoopAnimationsModule,
    FormsModule,
    HttpModule,
    TranslateModule.forRoot(),
    NgbModule.forRoot(),
    CoreModule,
    SharedModule,
    HomeModule,
    AboutModule,
    LoginModule,
    NgxChartsModule,
    AppRoutingModule
  ],
  declarations: [AppComponent],
  providers: [ ],
  bootstrap: [AppComponent]
})
export class AppModule { }

最佳答案

您必须考虑两件事:

您的应用程序只有一个根注入(inject)器,其中包含应用程序中任何模块(包括 AppModule)的 @NgModule.providers 数组中指定的所有提供程序。 每个组件都有自己的注入(inject)器(根注入(inject)器的子注入(inject)器),其中包含在组件的 @Component.providers 数组中声明的提供程序。 当 Angular 想要解析服务(MainService)的依赖项(RightClickService)时,他会在包含所有 NgModule 提供者的根注入(inject)器中查找它。

当 Angular 想要解析组件(RightClickComponent)的依赖关系(RightClickService)时,他会在组件注入(inject)器中查找它,如果没有找到,他会在父组件注入(inject)器中查找它,如果没有找到,他会执行相同的操作,直到到达如果未找到根注入(inject)器,则会抛出错误。

所以,我面临着同样的问题,这非常令人恼火,但当我在组件模块文件中定义依赖项时,问题最终得到了解决。

PS:解释自here

关于typescript - 从共享模块注入(inject)组件时,Angular 5 静态注入(inject)器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48727675/

相关文章:

angular - 在 Angular 中下载具有原样文件名的 Excel 文件

jquery - 使用 jQuery 的自定义 javascript 代码无法正常工作 Angular 5 (angular-cli)

javascript - 如何监听 Angular 1.5 组件中的作用域事件?

Angular 4 - 将对象从一个组件传递到另一个组件(无父子层次结构)

typescript - VSCode 和 TypeScript - 在不打开受影响文件的情况下重构/重命名

javascript - 类型错误 : Cannot read property 'map' of undefined react component

typescript - 我可以使用 typescript 在生成的 javascript 中添加额外的评论吗?

angular 5 adal.js 自动 token 更新负载 Angular 两次或更多

node.js - 如何配置 webpack 仅构建运行 Angular 5 应用程序所需的资源?

dart - Material 扩展面板的数据驱动内容