RequireJS的Angular 2导入错误

时间:2016-01-28 09:23:50

标签: asp.net typescript requirejs visual-studio-2015 angular

由于以下错误,我无法导入Angular2 / core:

Module name "angular2/core" has not been loaded yet for context: _. Use require([])

这是我的手稿文件:

import {Component, View} from "angular2/core";
import {bootstrap} from "angular2/platform/browser";
@Component({
          selector: "menu"
})
class MenuComponent {
isActive(path: string) {
    return true
   }
}
bootstrap(MenuComponent);

使用此配置编译:

{  
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "outDir": "../wwwroot/js",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },  
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

这是已编译的Javascript文件:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("angular2/core");
var browser_1 = require("angular2/platform/browser");
var MenuComponent = (function () {
function MenuComponent() {
}
MenuComponent.prototype.isActive = function (path) {
    var page = window.location.pathname;
    if (path === undefined || path === '') {
        path = '';
    }
    var result = path === '' ? path === page[page.length - 1] : page[page.length - 1].indexOf(path) > -1;
    return result;
};
MenuComponent = __decorate([
    core_1.Component({
        selector: "menu"
    }), 
    __metadata('design:paramtypes', [])
], MenuComponent);
return MenuComponent;
})();
browser_1.bootstrap(MenuComponent);
//# sourceMappingURL=menu.js.map

我的javascript按此顺序导入:

<script type="text/javascript" src="~/lib/requirejs/require.js"></script>
<script type="text/javascript" src="~/lib/angular2-polyfills.js"></script>
<script type="text/javascript" src="~/lib/es6-shim.js"></script>
<script type="text/javascript" src="~/lib/system-polyfills.js"></script>
<script type="text/javascript" src="~/lib/angular2-polyfills.js"></script>
<script type="text/javascript" src="~/lib/system.src.js"></script>
<script type="text/javascript" src="~/lib/rx.js"></script>
<script type="text/javascript" src="~/lib/angular2.js"></script>
<script type="text/javascript" src="~/js/menu.js"></script>

我想知道导致上述错误的是什么。因为我无法更改从Typescript文件编译的Javascript代码。

我还将 Visual Studio 2015 ASP.Net 5 一起使用。 我知道Angular 2仍然处于测试阶段且ASP.Net 5仍处于RC状态,但我怀疑这种组合是否会导致问题。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我认为你使用两个模块加载器,RequireJS和SystemJS。查看HTML文件中包含的内容:

<script type="text/javascript" src="~/lib/requirejs/require.js"></script> <------
<script type="text/javascript" src="~/lib/angular2-polyfills.js"></script>
<script type="text/javascript" src="~/lib/es6-shim.js"></script>
<script type="text/javascript" src="~/lib/system-polyfills.js"></script>
<script type="text/javascript" src="~/lib/angular2-polyfills.js"></script>
<script type="text/javascript" src="~/lib/system.src.js"></script> <------
<script type="text/javascript" src="~/lib/rx.js"></script>
<script type="text/javascript" src="~/lib/angular2.js"></script>
<script type="text/javascript" src="~/js/menu.js"></script>

你只需要一个。例如SystemJS。这就是消息的原因。两个库都尝试加载angular2/core模块...

修改

如果您只想使用SystemJS,可以更新tsconfig.json文件:

{  
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es5",
    "module": "system",  <--------
    "moduleResolution": "node",   <--------
    "outDir": "../wwwroot/js",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },  
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

希望它可以帮到你, 亨利