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