这来 self 在 https://stackoverflow.com/a/48436147/1019307 的回答.
我需要将转译器 VS 使用的模块类型更改为 AMD
.默认为 commonjs
即使当 <TypescriptModuleKind>
在 project.csproj
中更改来自 commonjs
成为AMD
.
查看问题:
- 添加 typescript 插件 - Typescript 工具、Typescript 构建和 Rich Newman 的 Typescript HTML 应用程序模板。
- 使用模板创建新项目
- 观察
project.csproj
包含<TypescriptModuleKind>commonjs</TypescriptModuleKind>
就这样吧 - 启动服务器。它启动正常,可以看到应用程序正在运行(开发人员控制台中没有错误消息)
打开
app.js
文件并观察它以:"use strict"; var Greeter = /** @class */ (function () { function Greeter(element) {
打开
app.ts
并以export
为前缀让它成为export class Greeter {
- 刷新页面并观察错误
exports is undefined
在开发者控制台中 打开
app.js
文件并观察它现在说以下内容:"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Greeter = /** @class */ (function () {
创建
tsconfig.json
通过运行tsc --init
- 观察它包含
"module": "commonjs"
- 运行
tsc
转译 ts 文件 - 观察
app.js
文件仍然如上(并且会导致exports is not defined
错误) - 更改
tsconfig.json
文件,使其包含"module": "amd"
- 运行
tsc
转译 ts 文件 打开
app.js
文件并观察它现在说以下内容:define(["require", "exports"], function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Greeter = /** @class */ (function () {
关注https://stackoverflow.com/a/48436147/1019307设置使用
requirejs
:-)- 刷新并观察它在浏览器中运行,没有显示任何错误
- 移动
tsconfig.json
通过将例如重命名为xxxxtsconfigxx.xxjson
来避开 - 停止服务器;修改
app.ts
归档使其变脏;重启服务器 - 观察
exports is not defined
错误返回 - 如果您愿意,可以将文件重命名为
tsconfig.json
并重复最后一步,观察错误仍然存在。这确认 VS 不使用本地tsconfig.json
用于配置。 - 修改
project.csproj
包含<TypescriptModuleKind>amd</TypescriptModuleKind>
- 停止服务器;修改
app.ts
归档使其变脏;重启服务器 - 观察
exports is not defined
错误返回
从而证明 VS 不遵守 tsconfig.json
文件,似乎忽略了它的 TypescriptModuleKind
配置值。
What are the TypeScript project build configuration options?好像说VS是怎么用tsc的。我使用 --module
运行切换并正常工作(例如 tsc --module commonjs [app.ts]
和 tsc --module amd [app.ts]
)。与 [app.ts]
如果 tsconfig.json
是必需的文件不存在。因此,VS 似乎忽略了 TypescriptModuleKind
。以应有的方式设置或不应用它。
那么这里有什么交易。有什么方法可以让它使用 tsconfig.json
归档或兑现其TypescriptModuleKind
设置?
最佳答案
我找到了答案。
在项目的属性中,有一个 Typescript Build
(侧面)选项卡,还有一个标记为 Module System
的下拉菜单,我将其设置为“AMD”。这奏效了!太棒了。
@Chris Halcrow 在评论中建议,如果您从项目文件中取出 Typescript 配置,则 VS 可能会使用 tsconfig.json
中的配置。我没有尝试过,但如果您需要比 VS 提供的选项更多的选项,那绝对值得一试。
我刚找到 https://developercommunity.visualstudio.com/content/problem/24584/cant-compile-the-ts-files-on-vs2017-node-modules-n.html ,其中包含一些有关 VS 如何处理 typescript 的有用信息。
我还意识到 @ChrisHalcrow 说的是正确的,他说“如果项目文件夹中有 tsconfig.json,那么 VS TS Config 将被忽略”。我没有强制 VS 识别 tsconfig.json
文件。一旦我再次添加它,它就会被看到。我忘记了 VS 需要“添加”东西(源文件、配置文件等)。我习惯于 Eclipe 和 IDEA 看到变化并询问或只是做。
更棒!
关于javascript - Visual Studio 2017 无法更改 project.csproj 的 <TypescriptModuleKind> 以进行 typescript 转译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494104/