代码之家  ›  专栏  ›  技术社区  ›  Dave Nottage

使用包中的可注入时“无法解析”的所有参数

  •  0
  • Dave Nottage  · 技术社区  · 7 年前

    我知道还有很多问题和我的相似,但是我找不到任何符合我的情况的问题。我有一个包,是共同的几个应用程序。在这个软件包中有一个名为AppReadyEvent的可注入程序,它基于本文:

    https://www.bennadel.com/blog/3151-revisited-creating-an-event-driven-pre-bootstrap-loading-screen-in-angular-2-0-0.htm

    我创建了原始软件包的精简版本,精简版本只包含以下内容:

    appreadyservice.ts公司

    import { Injectable, Inject } from '@angular/core';
    import { DOCUMENT } from "@angular/common"; // EDIT - was: @angular/platform-browser
    
    @Injectable()
    export class AppReadyEvent {
    
      private doc: Document;
      private isAppReady: boolean;
    
      constructor(@Inject(DOCUMENT) doc: any ) {
        this.doc = doc;
        this.isAppReady = false;
      }
    
      public trigger(): void {
        if ( this.isAppReady ) {
          return;
        }
        let bubbles = true;
        let bubbles2 = true;
        let cancelable = false;
        this.doc.dispatchEvent( this.createEvent('appready', bubbles, cancelable, bubbles2) );
        this.isAppReady = true;
      }
    
      private createEvent(eventType: string, bubbles: boolean, cancelable: boolean, bubbles2: boolean): Event {
        try {
          var customEvent: any = new CustomEvent(
            eventType,
            { bubbles: bubbles,
              cancelable: cancelable }
          );
        } catch ( error ) {
          var customEvent: any = this.doc.createEvent( 'CustomEvent' );
          customEvent.initCustomEvent( eventType, bubbles, cancelable, bubbles2);
        }
        return( customEvent );
      }
    }
    

    索引.ts:

    import { NgModule, ModuleWithProviders } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { AppReadyEvent } from './src/appreadyservice';
    export * from './src/appreadyservice';
    
    @NgModule({
      declarations: [
      ],
      imports: [
        CommonModule
      ],
      exports: [
      ]
    })
    export class TestModule {
      static forRoot(): ModuleWithProviders {
        return {
          ngModule: TestModule,
          providers: [AppReadyEvent]
        };
      }
    }
    

    包.json:

    {
      "name": "test-package-a6",
      "version": "1.0.0",
      "description": "Testing Packages with Angular 6",
      "main": "./dist/index.js",
      "types": "./dist/index.d.ts",
      "scripts": {},
      "author": "Dave Nottage",
      "license": "MIT",
      "dependencies": {
        "@angular/cli": "^6.0.8",
        "@angular/common": "^6.0.5",
        "@angular/core": "^6.0.5",
        "@angular/platform-browser": "^6.0.5"
      }
    }
    

    tsconfig.json文件:

    {
      "compilerOptions": {
        "skipLibCheck": true,
        "noImplicitAny": false,
        "module": "commonjs",
        "target": "ES5",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true,
        "declaration": true,
        "outDir": "./dist",
        "typeRoots": [
          "node_modules/@types"
        ]
      },
      "exclude": [
        "node_modules",
        "dist",
        "**/*.spec.ts"
      ],
      "angularCompilerOptions": {
        "genDir": "compiled"
      }
    }
    

    在测试项目中:

    应用模块ts:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    应用组件.ts:

    import { Component } from '@angular/core';
    import { AppReadyEvent } from 'test-package-a6';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
      providers: [AppReadyEvent]
    })
    export class AppComponent {
      title = 'app';
    }
    

    一旦我有了这么多,我就用:

    ng build --prod
    

    接受恐惧:

    ERROR in : Can't resolve all parameters for AppReadyEvent in D:/DEVELOP/test_a6/node_modules/test-package-a6/dist/src/appreadyservice.d.ts: (?).
    

    有什么线索吗?

    0 回复  |  直到 7 年前
    推荐文章