在 Visual Studio 中使用 angular2 [英] Use angular2 with visual studio
本文介绍了在 Visual Studio 中使用 angular2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人让 Angular2(测试版)在 Visual Studio 中工作?
我使用的是更新 1 的 Visual Studio 2015,并且我有 n 个 HTMLTypeScript 项目.
如果我将模块系统"设置为系统",我会收到构建错误(例如:找不到模块角度/核心"),但它会在保存时(而不是在构建时)编译 ts 文件.如果我已经运行了应用程序,我可以刷新页面并且它可以工作.
如果我将模块系统"设置为CommonJS",它将正确构建,但会出现运行时错误,例如‘require’未定义"和无法获取未定义或空引用的属性‘split’".
我认为我应该使用 system 因为这在使用 tsconfig.json 和教程中使用的 lite-server(带有 npm start)时有效.
解决方案
我在 Visual Studio 2015 中使用 angular2.我必须安装
您会注意到我将我的 app 文件夹和 ts 文件放在我的 wwwroot 中 - 这只是我的偏好,它遵循 angular2 快速入门.我将节点模块中的捆绑文件复制到 wwwroot 内的脚本文件夹中.
我基于 angular2 beta 快速入门创建了一个 package.json:
<代码>{"name": "angular2-quickstart",版本":1.0.0","许可证": "ISC",依赖关系":{"angular2": "2.0.0-beta.0","systemjs": "0.19.6","es6-promise": "^3.0.2","es6-shim": "^0.33.3",反射元数据":0.1.2","rxjs": "5.0.0-beta.0",zone.js":0.5.10"}}
然后我右键单击 package.json 并选择Restore Packages"并运行 NPM 安装以创建包含您看到的所有包的 node_modules 文件夹.然后我像这样设置我的 tsconfig.json 并将其放在根目录中:
<代码>{编译器选项":{目标":es5",模块":系统","moduleResolution": "节点",源地图":真,emitDecoratorMetadata":真,"experimentalDecorators": 真,删除评论":假,"noImplicitAny": 假},排除": [节点模块"]}
这是让它发挥作用的关键.我实际上不得不重新加载解决方案以查看 tsconfig.json 并开始使用它.除此之外,我进入我的工具>选项并设置打字稿选项,如下所示:
接下来我添加了我的 index.html 文件:
<头><meta charset="utf-8"/><title>示例 Angular 2 应用</title>头部><身体><my-app>正在加载...</my-app><script src="scripts/angular2-polyfills.js"></script><script src="scripts/system.src.js"></script><script src="scripts/Rx.js"></script><script src="scripts/angular2.dev.js"></script><脚本>系统配置({包:{应用程序: {格式:'注册',默认扩展名:'js'}}});System.import('app/boot').then(null, console.error.bind(console));