angular2 quickstart ReferenceError: angular is not

2019-09-17 08:28发布

问题:

I'm starting to take a look at angular2, but with this simple code

<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>

I've got this ugly errors:

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({

回答1:

I'm assuming you're running this in FireFox? Chrome doesn't seem to have an issue with it.

The problem is that the compiled Angular has an error in it. If you want to continue working in FireFox, download the angular2.sfx.dev.js and modify line 5610 from this:

function onGlobalPostMessage(event) {

To this:

var onGlobalPostMessage = function(event) {

That should get you working again.

I recommend using alpha.21 which is out now, btw, or you can use alpha.20 which is compiled here.



标签: angular