angular2 ReferenceError: angular is not defined new angular.Component({

angular2 ReferenceError: angular is not defined new angular.Component({

我开始看angular2, 但是用这个简单的代码

<html>
    <head>
            <script src="https://code.angularjs.org/2.0.0-alpha.19/angular2.sfx.dev.js"></script>
            <script>
            function AppComponent() {}

            AppComponent.annotations = [
                new angular.Component({
                        selector: 'my-app'
                }),
                new angular.View({
                        template: '<h1>My first Angular 2 App</h1>'
                })
            ];

            document.addEventListener('DOMContentLoaded', function() {
              angular.bootstrap(AppComponent);
            });
            </script>
    </head>
    <body>
        <my-app></my-app>
    </body>
</html>

我遇到以下错误:

SyntaxError: in strict mode code, functions may be declared only at top level or immediately within another function function onGlobalPostMessage(event) {

ReferenceError: angular is not defined new angular.Component({

我假设您在 FireFox 中使用 运行? Chrome 似乎没有问题。

问题是编译的Angular里面有错误。如果您想继续在 FireFox 中工作,请下载 angular2.sfx.dev.js 并从此修改第 5610 行:

function onGlobalPostMessage(event) {

为此:

var onGlobalPostMessage = function(event) {

这应该能让你重新开始工作。

我建议使用 alpha.21 which is out now, btw, or you can use alpha.20 which is compiled here