2

$rootScope の次の RootScopeService ラッパーを以下のように作成し、その後 xService などの名前の angular2 サービスの下でこれを使用しました。

import {Injectable, Inject} from "@angular/core";

@Injectable()
export class RootScopeService {
    static instance: RootScopeService;
    public $rootScope: any
    // constructor( @Inject("$rootScope") public $rootScope: any) 
    constructor() {
        return RootScopeService.instance = RootScopeService.instance || this;
    }
    public broadcast(eventName, event, scope) {
    this.$rootScope.$broadcast(eventName, event, scope);
    };
    public getRootScope(): any {
        return RootScopeService.instance.$rootScope;
    };
}

import {Injectable, Inject} from '@angular/core'
import {Logger, Level} from 'angular2-logger/core'

@Injectable()
export class xService {
    private log: Logger;
    private static instance: xService;
    constructor( @Inject("$rootScope") public rootScopeService: any) {
        if (xService.instance == null) {
            this.log = new Logger();
            this.log.Level = Level.DEBUG;
            xService.instance = this;
        }
        return xService.instance;
    }
    trackCustomEvent(customEvent, currentScope) {
        try {
            xService.instance.rootScopeService.broadcast(customEvent, currentScope);
        } catch (ex) {
            x.instance.log.error("x:: trackCustomEvent : " + ex.toString());
        }
    }
    trackActionEvent(actionEvent, scope) {
        xService.instance.trackCustomEvent(actionEvent, scope);
    }
}

AOT でコンパイルしましたが、問題なくコンパイルされています。その後、ロールアップでビルドファイルを作成しました。build.jsファイルを実行すると次の問題が発生します

Uncaught TypeError: Cannot read property 'get' of undefined
rootScopeFactory @ build.js:31452
__rootScope_46 @ build.js:80486
_RootScopeService_53 @ build.js:80528
createInternal @ build.js:80897
NgModuleInjector.create @ build.js:9964
NgModuleFactory.create @ build.js:9938
(anonymous function) @ build.js:9492
e.invoke @ zone.min.js:1inner.inner.fork.onInvoke @ build.js:8897
e.invoke @ zone.min.js:1n.run @ zone.min.js:1NgZone.run @ build.js:8766
PlatformRef_._bootstrapModuleFactoryWithZone @ build.js:9490
PlatformRef_.bootstrapModuleFactory @ build.js:9473
(anonymous function) @ build.js:81349
(anonymous function) @ build.js:81355
4

0 に答える 0