未捕获的ReferenceError:在angular2

时间:2016-01-19 10:34:03

标签: angular angular2-routing

<html>
<head>
<title>Angular 2 QuickStart</title>
<!-- 1. Load libraries -->
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/rxjs/bundles/Rx.umd.js"></script>
<script src="node_modules/angular2/bundles/angular2-all.umd.js"></script>
<!-- 2. Load our 'modules' -->
<script src='app/app.component.js'></script>
<script src='app/boot.js'></script>

<!--upgrade-->
<script src="node_modules/angular2/upgrade.js"></script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>

没有<script src="node_modules/angular2/upgrade.js"></script>代码可以正常工作。当我包括upgrade.js.it时显示“未捕获的ReferenceError:require未在angular2中定义”。如何克服此错误。

2 个答案:

答案 0 :(得分:-1)

require函数由SystemJS提供。您需要将其添加到script

<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/rxjs/bundles/Rx.umd.js"></script>
<script src="node_modules/angular2/bundles/angular2-all.umd.js"></script>
(...)

这是一个描述工作样本的plunkr:https://plnkr.co/edit/JXLDFBW4A1mi9tyNHoJ3?p=preview

希望它可以帮到你, 亨利

答案 1 :(得分:-3)

模块应该是tsconfig.json中的系统

{
    "version": "1.0.0",
    "compilerOptions": {
        "target": "es5",
        **"module": "system",**
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "noLib": false,
        "declaration": false
    },
    "exclude": [
        "node_modules",
        "bower_components",
        "jspm_packages",
        "typings/main",
        "typings/main.d.ts"
    ]
}