【发布时间】:2019-05-08 20:42:05
【问题描述】:
我有一个功能性 AngularJS 1.7 应用程序,其中我的所有代码都是用 TypeScript 编写的。但总有一些事情困扰着我。
在我的 app.module.ts 文件中,我有一点丑陋:declare var angular;
为了this.app = angular.module('app'...
转译和运行。
我尝试过的事情:
1) 将declare var angular; 替换为import angular from "angular";
转译很好,但随后浏览器抱怨Uncaught (in promise) TypeError: angular_1.default.module is not a function
2) 将declare var angular; 替换为import * as angular from "angular";
也可以正常编译,但浏览器会出现类似的错误:Uncaught (in promise) TypeError: angular.module is not a function
3) 将declare var angular; 替换为import ng from "angular";
然后使用 ng.angular.module 或 ng.module 根本不会转译。
我确定我已经尝试了其他一些方法,但我能够让事情正常工作的唯一方法是使用 declare var angular;
这一切都很好,但这对我来说肯定闻起来很糟糕。为什么我必须这样做?我究竟做错了什么?有没有更好的方法?
详情:
- Visual Studio 2017/2019
- TypeScript 3.3
- SystemJS(不是 RequireJS)
- AngularJS 1.7.8
- @types/angular 1.6.54
package.json
"devDependencies": {
"@types/angular": "^1.6.54",
...
},
"dependencies": {
"angular": "~1.7.8",
...
}
tsconfig.json
{
"compileOnSave": true,
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [ "node_modules/types/*", "node_modules/*" ],
},
"module": "system",
"target": "es6",
"sourceMap": true,
"lib": [ "es6", "dom" ],
"allowSyntheticDefaultImports": true,
"outFile": "./app/everything.js",
"moduleResolution": "node",
"types": [
"angular",
"jquery"
]
},
"include": ["app/**/*"],
"exclude": ["node_modules", "lib"],
"strict": true
}
app.module.ts
declare var angular;
...
export class App {
app: ng.IModule;
constructor() {
this.app = angular.module('app', [
...
]);
}
public run() {
this.app.run([
...
]);
...
}
index.html
...
<script src="lib/systemjs/system.js"></script>
<script src="lib/angular/angular.min.js" type="text/javascript"></script>
...
<script src="app/everything.js" type="text/javascript"></script>
<script>
System.config({
map: {
"angular": "lib/angular",
...
},
packages: {
"angular": { main: "angular.min.js", defaultExtension: "js" },
...
}
});
System.import("app.module")
.then(function (app) {
let a = new app.App();
a.run();
angular.element(function () {
angular.bootstrap(document, ['app']);
});
});
</script>
【问题讨论】:
-
请花点时间阅读How to Ask,尤其是标题为“写一个总结具体问题的标题”的部分。你现在的头衔...没有那样做:)。
-
是的,标题不太好。编辑了它,希望它现在更好。谢谢!
标签: angularjs typescript systemjs