[AngularJS]无法运行我的角 2 应用程序时使用事件发射器

发布时间: 2017/4/16 20:44:41
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我遇到一个问题我的应用程序将不再加载后添加一项服务,利用事件发射器。 我得到的错误是,如下所示︰

"SyntaxError: expected expression, got '<'
Evaluating http://localhost:3000/node_modules/angular2/ts/src/facade/async
Error loading http://localhost:3000/app/main.js" angular2-polyfills.js:138:14

SyntaxError: expected expression, got '<'

我哪里应用引导的 Main.ts 文件看起来像这样︰

import {bootstrap} from "angular2/platform/browser"
import {provide} from "angular2/core";
import {ROUTER_PROVIDERS} from "angular2/router"

import {AppComponent} from "./app.component"
import {LocalStorageService} from "./core/localstorage.service";
import {PersistenceService} from "./core/persistence.service";

bootstrap(AppComponent, [
    ROUTER_PROVIDERS,
    LocalStorageService,
    provide(PersistenceService, {useExisting: LocalStorageService})
]);

这里我使用的事件发射器的服务看起来像这样︰

import {Injectable, Output} from "angular2/core";
import {EventEmitter} from "../../node_modules/angular2/ts/src/facade/async";

@Injectable()
export class GlobalActionsService {
    @Output() saveSettings: EventEmitter<any> = new EventEmitter();
    @Output() loadSettings: EventEmitter<any> = new EventEmitter();

    doSave() {
        this.saveSettings.emit(null);
    }

    doLoad() {
        this.loadSettings.emit(null);
    }

}

我 index.html 最后,看起来像这样︰

<html>
    <head>
        <!-- Set the base href for routing -->
        <base href="/">

        <title>MyApp</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <!-- 1. Load stylesheets -->
        <link rel="stylesheet" type="text/css" href="./public/bootstrap/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="./public/css/custom.css">

        <!-- 2. Load libraries -->
        <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
        <script src="node_modules/systemjs/dist/system.src.js"></script>
        <script src="node_modules/rxjs/bundles/Rx.js"></script>
        <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
        <script src="node_modules/angular2/bundles/router.dev.js"></script>

        <!-- 3. Configure SystemJS -->
        <script>
            System.config({
                packages: {
                    app: {
                        format: 'register',
                        defaultExtension: 'js'
                    }
                }
            });
            System.import('app/main').then(null, console.error.bind(console));
        </script>
    </head>

    <!-- 4. Display the application -->
    <body>
        <my-app>Loading...</my-app>
    </body>
</html>

我不确定如果我的问题关乎如何我引导应用程序、 使用我的事件发射器或别的东西完全...如果任何人都可以更加清楚一些问题可能所说,在这里,它将不胜感激 !

解决方法 1:

您需要导入 EventEmitter 这种方式 (从 Angular2 的核心模块)︰

import {Injectable, Output, EventEmitter} from "angular2/core";
赞助商