如何在没有构建“错误"的情况下在 Visual Studio 2017 中使用 Angular-CLI [英] How to use Angular-CLI with Visual Studio 2017 Without Build "Errors"

查看:26
本文介绍了如何在没有构建“错误"的情况下在 Visual Studio 2017 中使用 Angular-CLI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是在许多项目(其中一个是针对 es5 的 angular-cli 项目)的解决方案中,由于 Microsoft 开发了 Typescript,他们过于急于尝试在不完全了解 angular 的情况下进行编译-cli 故事.因此 (TS) 错误在构建之间经常令人沮丧地突然出现,并隐藏了否则将是有效错误的内容.

The problem I've encountered is in a solution with many projects (one of them being a angular-cli project targeting es5) is that, since Microsoft developed Typescript, they are over eager to attempt to compile without fully knowing the angular-cli story. Therefore (TS) errors frustratingly crop up frequently between builds and hide what would otherwise be a valid error.

如何确保 Visual Studio 将忽略这些 Typescript 错误并且在构建后完全不显示它们?

How can I ensure that Visual Studio will ignore these Typescript errors and not show them at all post-build?

推荐答案

事实证明,由于 angular-cli 使用自己的配置文件进行构建,因此修改 Microsoft 的 tsconfig.json 文件是安全的在构建之前检查.我的解决方案是修改我的 tsconfig.json 如下:

Turns out, since angular-cli uses it's own configuration file for builds, it's safe to modify the tsconfig.json file that Microsoft checks before builds. My solution was to modify my tsconfig.json as follows:

{
  "exclude": [
    "node_modules/*",
    "src/*",
    "app/*",
    "@angular/*",
    "package.json"
  ],
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./wwwroot",
    ...
    "experimentalDecorators": true,
    "target": "es6",
  ...
  },
  "ignoreRules": {
    "TS2307": true,
    "TS2304": true,
    "TS2693": true
  }
}

有效地阻止 Visual Studio 编译,而是默认使用 ng-build(它本身被配置为输出到 wwwroot)这在 Visual Studio 中点击运行"按钮时也很不错.

Effectively blocking Visual Studio compilation and instead defaulting to ng-build (which itself is configured to output to wwwroot) Which also plays nice when hitting the "run" button in Visual Studio.

.csproj

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>

...

  <Target Name="DebugRunWebpack" BeforeTargets="Build">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="false">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Exec Command="npm install" />
    <Exec Command="ng build --env=$(Configuration)" />
  </Target> 

angular-cli.json

angular-cli.json

"apps": [
    {
      "root": "src",
      "outDir": "wwwroot",
...

还有一件事...

您还必须在 Startup.cs 文件中设置默认路径:

You'll also have to set the default path in your Startup.cs file:

 context.Request.Path = "/index.html";

这篇关于如何在没有构建“错误"的情况下在 Visual Studio 2017 中使用 Angular-CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆