简体   繁体   English

Angular2-未捕获的ReferenceError:ng未定义

[英]Angular2 - Uncaught ReferenceError: ng is not defined

I'm trying to run a basic Angular2 app. 我正在尝试运行基本的Angular2应用。 However currently running into the following error when I run the app with live-server and npm start : 但是,当我使用live-servernpm start运行应用程序时,当前遇到以下错误:

Uncaught ReferenceError: ng is not defined

在此处输入图片说明

I have the latest Angular2 npm installed. 我安装了最新的Angular2 npm。 Here is my markup: 这是我的标记:

<html>
    <head>
        <title></title>
        <script src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script>
        <script src="https://code.angularjs.org/tools/typescript.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script>

        <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
        <script src="app.js"></script>
    </head>
    <body>
        <first-comp></first-comp>
    </body>
</html>

My app.js file: 我的app.js文件:

var FirstComponent = ng.
    Component({
        selector: 'first-comp'  
    })
    .View({
        template: '<h2>I am learning {{ myFramework }}</h2>'
    })
    .Class({
        constructor: function() {
            this.myFramework = "Angular2";
        }
    });

document.addEventListener('DOMContentLoaded', function() {
    ng.bootstrap(firstComponent);
});

From: 从:

https://angular.io/guide/quickstart https://angular.io/guide/quickstart

you are missing the boot.js file which bootstrap's the whole thing before you start using it, in your file you are using the ng object before the DOM has loaded ( DOMContentLoaded ). 您会丢失boot.js文件,该文件在开始使用前会进行整个引导,而在文件中,您是在DOM加载之前使用ng对象( DOMContentLoaded )。 That's why you have the wrapper on: 这就是为什么要使用包装器的原因:

document.addEventListener('DOMContentLoaded', function() {
    ng.bootstrap(firstComponent);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM