未捕获(承诺):TypeError:令牌未定义

时间:2020-02-04 11:14:45

标签: angular typescript angular8

我有一个Angular 8应用程序,并且在开发模式下工作良好,但是如果我执行``ng serve --prod”,它会正确编译,但是在启动应用程序时,我在控制台中遇到了这个错误:

ERROR错误:“未捕获(承诺):TypeError:令牌未定义 getOrCreateInjectable / bloomHash <@ http://localhost:4200/main.cf6fe5006f05f980ad3b.js:1:249524 getOrCreateInjectable @ http://localhost:4200/main.cf6fe5006f05f980ad3b.js:1:249614 get @ http://localhost:4200/main.cf6fe5006f05f980ad3b.js:1:254632 PlayerComponent @ http://localhost:4200/main.cf6fe5006f05f980ad3b.js:1:726594 factory @ http://localhost:4200/main.cf6fe5006f05f980ad3b.js:1:728189 getNodeInjectable @ http://localhost:4200/main.cf6fe5006f05f980ad3b.js:1:253710 ...

您对如何确定出了什么问题有想法吗? 读取错误后,我调用了PlayerComponent,但是其中没有什么特别的

import { Game, IsoScene } from 'src/app/tars/classes';
import { NavigatorService } from 'src/app/tars/services';
import { DataProvider, ElementProvider } from 'src/app/tars/providers';
import { ElementProvider as POCElementProvider } from 'src/app/services/element.provider';
import { IsoPlayerScene } from 'src/app/classes/scenes/iso';
import { InteractionsMenu } from 'src/app/interfaces';
import { DataProviderFactory } from 'src/app/services/dataProviders/factory.service';
import { OnReadyRenderer2DEvent, Interaction } from 'src/app/tars/interfaces';
import { TitleScene } from 'src/app/classes/scenes/title';

@Component({
  selector: 'tars-player',
  templateUrl: './player.component.html',
  styleUrls: ['./player.component.scss']
})
export class PlayerComponent {
  private rendererW: number;
  private rendererH: number;

  public get rendererHeight(): number {
    return this.rendererH;
  }

  public get rendererWidth(): number {
    return this.rendererW;
  }

  private game: Game;
  private navigatorService: NavigatorService;
  private dataProvider: DataProvider;
  private elementProvider: ElementProvider;

  private isoPlayer: IsoPlayerScene;
  public menuStructure: InteractionsMenu;

  constructor(
    private injector: Injector,
    private ngZone: NgZone
  ) {
    this.navigatorService = injector.get(NavigatorService);

    this.dataProvider = injector.get(DataProvider);
    this.dataProvider.set(injector.get(DataProviderFactory.factory()));

    this.elementProvider = injector.get(ElementProvider);
    this.elementProvider.set(injector.get(POCElementProvider));

    this.resetInteractionsMenu();
  }

  rendererOnReady(event: OnReadyRenderer2DEvent) {
    // Create the game object
    this.game = new Game(this.injector, event.context);
    this.game.onLoaded.subscribe((res: boolean) => {
      // Ask current client size
      this.refreshClientSize();
      // Run the game (when ready inside)
      this.ngZone.runOutsideAngular(() => this.game.run());
    });

    // Specific link
    this.isoPlayer = new IsoPlayerScene(this.injector, {name: 'world'}, event.context);
    this.isoPlayer.onInteractionsMenu.subscribe((structure: InteractionsMenu) => {
      this.ngZone.run(() => {
        if (structure) {
          this.menuStructure = structure;
        } else {
          this.resetInteractionsMenu();
        }
      });
    });

    this.game.load([
      this.isoPlayer,
      new TitleScene(this.injector, {name: 'title'}, event.context),
      new IsoScene(this.injector, {name: 'loader'}, event.context),
    ], 'title', 'loader');
  }

  // Listen the window resize event
  @HostListener('window:resize', ['$event'])
  onResize(event: any) {
    this.refreshClientSize();
    this.resetInteractionsMenu();
  }

  // Get the window/body size to set the graph size
  public refreshClientSize() {
    const browserSize = this.navigatorService.getClientSizeFromBrowser();
    this.rendererW = browserSize.width;
    this.rendererH = browserSize.height;
    this.navigatorService.setClientSize(browserSize.width, browserSize.height);
  }

  // Keyboard management
  @HostListener('window:keydown', ['$event'])
  onKeyPress(event: any) {
    this.navigatorService.keyDown(event);
  }

  private resetInteractionsMenu() {
    this.menuStructure = {
      interactions: [],
      screenCoord: { screenX: 0, screenY: 0 }
    };
  }

  // Do interaction when menu selection is done and reset menu
  menuOnSelect(interaction: Interaction) {
    this.isoPlayer.doInteraction(interaction);
    this.resetInteractionsMenu();
  }
}

就像我说的那样,它在开发人员中正常工作,我不明白为什么--prod给出了这个结果。

编辑:

似乎是关于工厂的第this.dataProvider.set(injector.get(DataProviderFactory.factory()));行,其下面的代码在--prod上中断了

@Injectable()
export class DataProviderFactory {
  public static factory() {
    if (environment.dataOrigin === DataOrigin.Fake) {
      return Faker;
    } else {
      // ...
    }
  }
}

如果我直接将Faker替换为DataProviderFactory.factory(),则它正常工作。 我读到有关Ivy和creatInjector的问题,但似乎不起作用。

有什么主意吗?

1 个答案:

答案 0 :(得分:0)

在--prod的上下文中,DataProviderFactory返回一个null值,该值是注入器无法处理以进行标记化的。所以失败了,我不好。但是,显示出来的异常一点也不清晰和有用。希望这可以帮助其他人。