0

I am aware there's a number of other questions that are similar to mine, however I'm unable to find any that match my situation. I have a package that is common to several applications. Within this package is an injectable called AppReadyEvent, which is based off this article:

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

I have created a cut-down version of the original package, and the cut-down version simply has this:

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 );
  }
}

index.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]
    };
  }
}

package.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"
  }
}

In the test project:

app.module.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 { }

app.component.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';
}

Once I had as much as that together, I use:

ng build --prod

and receive the dreaded:

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

Any clues?

Dave Nottage
  • 3,411
  • 1
  • 20
  • 57
  • check https://stackoverflow.com/questions/37521298/how-to-inject-document-in-angular-2-service – Fateme Fazli Jun 19 '18 at 05:05
  • Please be more specific about the link - are you referring to the fact that importing DOCUMENT from @angular/platform-browser is deprecated? Changing it to @angular/common doesn't make any difference in this case – Dave Nottage Jun 19 '18 at 05:18
  • Please be more specific about the answer and how it injects the Document not the deprecation. also i didn't answer and didn't mark as duplicate, i just put the comment and link to see that maybe helps. – Fateme Fazli Jun 19 '18 at 05:26
  • The link doesn't help me, since I don't know which part(s) might help. There's a line at the top of the accepted answer that might possibly help, however I have no idea where it should go, and the answer gives no clue as to where it should go. – Dave Nottage Jun 19 '18 at 05:42
  • may i ask why didn't you put AppReadyEvent in app.module.ts providers and put in app.component? – Fateme Fazli Jun 19 '18 at 06:05
  • Because it makes no difference to the result – Dave Nottage Jun 19 '18 at 06:28
  • i made a [stackblitz](https://stackblitz.com/edit/angular-mquf89) but i couldn't produce the error, you are doing it right. – Fateme Fazli Jun 19 '18 at 07:01
  • It only produces an error when the injectable is being consumed from a package, and when the --prod switch is used. Not sure whether either of those are even possible with StackBlitz – Dave Nottage Jun 19 '18 at 07:04

0 Answers0