Angular 错误:(SystemJS)未定义导出
我正在开发一个.net核心mvc+angular应用程序。mvc框架处理用户管理,当用户到达应用程序的特定区域时,angular接管。 因此,当我在网站上添加一个angular-2(统计)应用程序时,它开始工作了。我需要实现另一个功能,所以我在同一个解决方案中添加了另一个angular-2应用程序(practices)。一切都很好。角-4出现了。因此,更改了对angular-4的引用,因为我正在开发第二个angular应用程序,所以有一段时间没有检查第一个angular应用程序。现在,当我尝试运行第一个angular应用程序(stats)时,当第二个应用程序加载angular时,我得到了这个错误 这两个应用程序共享systemjs.config.js、package.json、tsconfig.json和typings.json 错误:Angular 错误:(SystemJS)未定义导出,angular,typescript,Angular,Typescript,我正在开发一个.net核心mvc+angular应用程序。mvc框架处理用户管理,当用户到达应用程序的特定区域时,angular接管。 因此,当我在网站上添加一个angular-2(统计)应用程序时,它开始工作了。我需要实现另一个功能,所以我在同一个解决方案中添加了另一个angular-2应用程序(practices)。一切都很好。角-4出现了。因此,更改了对angular-4的引用,因为我正在开发第二个angular应用程序,所以有一段时间没有检查第一个angular应用程序。现在,当我尝试运
Error: (SystemJS) exports is not defined
ReferenceError: exports is not defined
at eval (http://localhost:60660/apps/common/date.extentions.js:2:23)
at eval (<anonymous>)
at Object.eval (http://localhost:60660/apps/common/daterange.component.js:13:1)
at eval (http://localhost:60660/apps/common/daterange.component.js:40:4)
at eval (http://localhost:60660/apps/common/daterange.component.js:41:3)
at eval (<anonymous>)
Evaluating http://localhost:60660/apps/common/date.extentions.js
Evaluating http://localhost:60660/apps/common/daterange.component.js
Evaluating http://localhost:60660/apps/stats/app.module.js
Evaluating http://localhost:60660/apps/stats/main.js
Error loading http://localhost:60660/apps/stats/main.js
at eval (http://localhost:60660/apps/common/date.extentions.js:2:23)
at eval (<anonymous>)
at Object.eval (http://localhost:60660/apps/common/daterange.component.js:13:1)
at eval (http://localhost:60660/apps/common/daterange.component.js:40:4)
at eval (http://localhost:60660/apps/common/daterange.component.js:41:3)
at eval (<anonymous>)
Evaluating http://localhost:60660/apps/common/date.extentions.js
Evaluating http://localhost:60660/apps/common/daterange.component.js
Evaluating http://localhost:60660/apps/stats/app.module.js
Evaluating http://localhost:60660/apps/stats/main.js
Error loading http://localhost:60660/apps/stats/main.js
package.json
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"dependencies": {
"@angular/common": "~4.0.0",
"@angular/compiler": "~4.0.0",
"@angular/core": "~4.0.0",
"@angular/forms": "~4.0.0",
"@angular/http": "~4.0.0",
"@angular/platform-browser": "~4.0.0",
"@angular/platform-browser-dynamic": "~4.0.0",
"@angular/router": "~4.0.0",
"@types/node": "^7.0.13",
"angular-in-memory-web-api": "~0.3.0",
"core-js": "2.4.1",
"reflect-metadata": "0.1.8",
"rxjs": "5.3.0",
"spinkit": "1.2.5",
"systemjs": "0.19.39",
"typescript": "2.2.1",
"zone.js": "0.8.5"
},
"devDependencies": {
"@types/core-js": "^0.9.34",
"concurrently": "3.1.0",
"gulp": "3.9.1",
"gulp-concat": "2.6.0",
"gulp-cssmin": "0.1.7",
"gulp-environments": "0.1.1",
"gulp-rimraf": "0.2.0",
"gulp-uglify": "2.0.0",
"lite-server": "2.2.2",
"typescript": "2.2.1",
"typings": "~2.1.1"
}
}
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [ "es2015", "dom" ],
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"exclude": [
"node_modules",
"wwwroot/lib",
"typings"
]
}
typings.json
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160602141332",
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"node": "registry:dt/node#7.0.0+20170110233017"
}
}
date.extensions.ts
export { }
// DATE EXTENSIONS
// ================
declare global {
interface Date {
addDays(days: number, useThis?: boolean): Date;
isToday(): boolean;
clone(): Date;
isAnotherMonth(date: Date): boolean;
isWeekend(): boolean;
isSameDate(date: Date): boolean;
yyyy_mm_dd(): string;
}
}
Date.prototype.addDays = function (days: number): Date {
if (!days) return this;
console.log(this);
let date = this;
date.setDate(date.getDate() + days);
return date;
};
Date.prototype.isToday = function (): boolean {
let today = new Date();
return this.isSameDate(today);
};
Date.prototype.clone = function (): Date {
return new Date(+this);
};
Date.prototype.isAnotherMonth = function (date: Date): boolean {
return date && this.getMonth() !== date.getMonth();
};
Date.prototype.isWeekend = function (): boolean {
return this.getDay() === 0 || this.getDay() === 6;
};
Date.prototype.isSameDate = function(date: Date): boolean {
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};
Date.prototype.yyyy_mm_dd = function (): string {
var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return [this.getFullYear(), '-', mm.length === 2 ? '' : '0', mm, '-', dd.length === 2 ? '' : '0', dd].join(''); // padding
}
date.extensions.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Date.prototype.addDays = function (days) {
if (!days)
return this;
console.log(this);
var date = this;
date.setDate(date.getDate() + days);
return date;
};
Date.prototype.isToday = function () {
var today = new Date();
return this.isSameDate(today);
};
Date.prototype.clone = function () {
return new Date(+this);
};
Date.prototype.isAnotherMonth = function (date) {
return date && this.getMonth() !== date.getMonth();
};
Date.prototype.isWeekend = function () {
return this.getDay() === 0 || this.getDay() === 6;
};
Date.prototype.isSameDate = function (date) {
return date && this.getFullYear() === date.getFullYear() && this.getMonth() === date.getMonth() && this.getDate() === date.getDate();
};
Date.prototype.yyyy_mm_dd = function () {
var mm = (this.getMonth() + 1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return [this.getFullYear(), '-', mm.length === 2 ? '' : '0', mm, '-', dd.length === 2 ? '' : '0', dd].join(''); // padding
};
//# sourceMappingURL=date.extentions.js.map
从apps/common/date.extensions.ts中删除导出{}
,或者明确指定模块格式“cjs”
SystemJS使用模块格式检测来确定需要提供哪些变量才能加载模块。但是,它不会将您的date.extensions.js
识别为CommonJS,因为没有分配给module.exports
,也没有需要调用(1)
要显式设置模块格式,请添加
packages: {
"apps/common/date.extensions.js": {
"format": "cjs"
},
// ...
}
SystemJS现在将使用此元数据而不是启发式来确定格式,因此在加载模块时将提供一个exports
对象
或者,由于文件不需要是模块,因此可以删除导出{}
和声明全局
包装器
除此之外,这涉及正则表达式,它试图通过搜索模式(如/exports\)来检测CommonJS*“\s*=\s*+/g
。然而,这并不是万无一失的,特别是考虑到SystemJS检测到的某些模块格式是其他模块格式的部分超集。例如,AMD模块可以匹配相同的正则表达式,因为它提供了自己的CommonJS兼容层。这就是说,SystemJS模块格式检测功能强大,允许许多不协调的模块系统自动、正确地交互,无需配置。盖伊·贝德福德做了非常出色的工作
apps/common/date.extensions.js
中有什么内容?如果您将'apps/common/date.extensions.js'
添加到包中,并将格式:“cjs'
添加到包中,会怎么样?添加了date.extensions文件我看到了。摆脱导出{}
应该可以正常工作。我认为SystemJS的格式检测不会发现这一点,因为没有分配给模块。导出
,并且没有需要
。只需将其作为import'apps/common/date.extensions.js'
导入,而不使用中的。另外,请删除declare global
包装,因为在删除export{}
时,除非您感谢您的帮助,否则类型检查将失败。我会试试你最后的建议。但是想让你知道,添加格式:cjs也解决了这个问题。
packages: {
"apps/common/date.extensions.js": {
"format": "cjs"
},
// ...
}