简体   繁体   English

如何获得Visual Studio Typescript 1.7智能感知以使用Node.js库(Angular2,rxjs)?

[英]How can I get Visual Studio Typescript 1.7 intellisense to work with nodejs libraries (angular2, rxjs)?

[SOLVED] I logged a bug against TypeScript and the fix should be rolled up into the next VS plugin release. [已解决]我记录了一个针对TypeScript错误 ,该错误应汇总到下一个VS插件版本中。 Essentially you can get intellisense to work if you specify your module resolution to be "NodeJs". 本质上,如果您将模块分辨率指定为“ NodeJs”,则可以使智能感知工作。 However the compiler doesn't like this because it expects it to be "node". 但是,编译器不喜欢这样,因为它希望它是“节点”。 It looks like the fix they're making is to make everything work by specifying "Node". 看来他们要解决的问题是通过指定“节点”使所有工作正常。


I'm trying to build an Angular2 project with msbuild because I want to use ASP.Net Web API as my back-end. 我正在尝试使用msbuild构建Angular2项目,因为我想使用ASP.Net Web API作为后端。 The code compiles and runs fine, but my editor (SublimeText 3 or Visual Studio 2015) doesn't like the way I import the Angular2 libraries: 代码可以编译并正常运行,但是我的编辑器(SublimeText 3或Visual Studio 2015)不喜欢我导入Angular2库的方式:

boot.ts: boot.ts:

import {bootstrap} from 'angular2/platform/browser';  // error: cannot find module
import {AppComponent} from './app.component';
bootstrap(AppComponent, []);

If I run the same example ( Angular2 Hero Tutorial ) using the TypeScript compiler directly with a tsconfig.json and node lite-server as documented, then SublimeText3 works and can recognize this syntax just fine. 如果我直接使用TypeScript编译器通过tsconfig.json和node lite-server运行相同的示例(《 Angular2 Hero教程》 ),那么SublimeText3可以正常工作并且可以识别这种语法。 However once I switch to msbuild, I get this intellisense error for some reason. 但是,一旦我切换到msbuild,由于某种原因,我会收到此intellisense错误。 Here's other pieces of my project which I think are relevant: 我认为这是我项目中其他相关的部分:

Folder structure: 资料夹结构:

\
  - index.html
  - package.json
  \app
      - boot.ts
      - app.component.ts
  \node_modules
      \angular2
          \platform
              - browser.js
              - browser.d.ts

package.json: 的package.json:

{
  "name": "angular2-webapi",
  "version": "1.0.0",
  "scripts": {
  },
  "license": "ISC",
  "dependencies": {
    "angular2": "2.0.0-beta.0",
    "systemjs": "0.19.6",
    "es6-promise": "^3.0.2",
    "es6-shim": "^0.33.3",
    "reflect-metadata": "0.1.2",
    "rxjs": "5.0.0-beta.0",
    "zone.js": "0.5.10"
  },
  "devDependencies": {
    "typescript": "^1.7.3" 
  }
}

TypeScript compiler section from csproj file (most of this was copied from the Angular2 Hero tutorial tsconfig file, but some of these were set by VS automatically): csproj文件的TypeScript编译器部分(其中大部分是从Angular2 Hero教程 tsconfig文件复制的,但是其中一些是由VS自动设置的):

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
  <TypeScriptTarget>ES5</TypeScriptTarget>
  <TypeScriptJSXEmit>None</TypeScriptJSXEmit>
  <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
  <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
  <TypeScriptModuleKind>System</TypeScriptModuleKind>
  <TypeScriptRemoveComments>False</TypeScriptRemoveComments>
  <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
  <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
  <TypeScriptSourceMap>True</TypeScriptSourceMap>
  <TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>
  <TypeScriptModuleResolution>node</TypeScriptModuleResolution>
  <TypeScriptEmitDecoratorMetadata>true</TypeScriptEmitDecoratorMetadata>
</PropertyGroup>          

I've also tried changing the way I reference the Angular2 libraries like below since it matches the actual physical path of the browser script, but unfortunately the generated file doesn't add the ".js" extension to the generated dependency list for some reason. 我还尝试过更改如下所示的引用Angular2库的方式,因为它与浏览器脚本的实际物理路径匹配,但是不幸的是,由于某种原因,生成的文件未将“ .js”扩展名添加到生成的依赖项列表中。 This means that the browser will request the file "browser" instead of "browser.js". 这意味着浏览器将请求文件“ browser”而不是“ browser.js”。

This breaks the runtime: 这会破坏运行时:

import {bootstrap} from '../node_modules/angular2/platform/browser';

I'm actually not sure how the system is supposed to know how to resolve the 'angular2/platform/browser' syntax, but it seems to work if I don't use msbuild. 我实际上不确定系统应该如何知道如何解析“ angular2 / platform / browser”语法,但是如果我不使用msbuild,它似乎可以工作。 And given the fact that this code compiles and runs properly, it seems like it should work in my editor as well. 考虑到该代码可以正确编译和运行,看来它也应该在我的编辑器中也可以工作。 Anyone have any ideas? 有人有想法么?

As far as project property group is set to 至于项目属性组设置为

<TypeScriptModuleResolution>NodeJs</TypeScriptModuleResolution>

there is a workaround is to modify Microsoft.Typescript.targets . 有一种解决方法是修改Microsoft.Typescript.targets It makes compiler to work: 它使编译器可以工作:

<TypeScriptBuildConfigurations Condition="'$(TypeScriptModuleResolution)' != ''">$(TypeScriptBuildConfigurations) --moduleResolution $(TypeScriptModuleResolution)</TypeScriptBuildConfigurations>

hardcoded as 硬编码为

<TypeScriptBuildConfigurations Condition="'$(TypeScriptModuleResolution)' != '' and '$(TypeScriptModuleResolution)' != 'NodeJs'">$(TypeScriptBuildConfigurations) --moduleResolution $(TypeScriptModuleResolution)</TypeScriptBuildConfigurations>
<TypeScriptBuildConfigurations Condition="'$(TypeScriptModuleResolution)' == 'NodeJs'">$(TypeScriptBuildConfigurations) --moduleResolution node</TypeScriptBuildConfigurations>

PS make sure to take a backup of Microsoft.Typescript.targets PS请确保备份Microsoft.Typescript.targets

This does appear to be a bug in typescript tooling as @AndrewSorokin describes, but instead of editing Microsoft.Typescript.targets , you can override the incorrect code in your project file. 正如@AndrewSorokin所描述的,这确实是打字稿工具中的错误,但您无需编辑Microsoft.Typescript.targets ,而可以覆盖项目文件中的错误代码。 Add the following lines to the end of your project file: 将以下行添加到项目文件的末尾:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->
  <!-- Set the Module Resolution mode to be NodeJs as required by intellisense -->
  <!-- See http://stackoverflow.com/questions/34939261/how-can-i-get-visual-studio-typescript-1-7-intellisense-to-work-with-nodejs-libr -->
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
    <TypeScriptModuleResolution>NodeJs</TypeScriptModuleResolution>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
    <TypeScriptModuleResolution>NodeJs</TypeScriptModuleResolution>
  </PropertyGroup>
  <!-- ... -->
  <!-- Override the moduleResolution parameter to tsc to overcome a bug in intellisense -->
  <!-- See http://stackoverflow.com/questions/34939261/how-can-i-get-visual-studio-typescript-1-7-intellisense-to-work-with-nodejs-libr -->
  <PropertyGroup>
    <CompileTypeScriptDependsOn>
      SpoofTypeScriptModuleResolution;
      $(CompileTypeScriptDependsOn)
    </CompileTypeScriptDependsOn>
  </PropertyGroup>
  <Target Name="SpoofTypeScriptModuleResolution">
    <PropertyGroup>
      <TypeScriptBuildConfigurations>$(TypeScriptBuildConfigurations.Replace('--moduleResolution NodeJs', '--moduleResolution node'))</TypeScriptBuildConfigurations>
    </PropertyGroup>
    <Message Text="Options: $(TypeScriptBuildConfigurations)" />
  </Target>
</Project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio中打开独立的angular2和TypeScript项目 - How to open standalone angular2 and TypeScript project in visual studio JavaScript Intellisense与Visual Studio代码:如何让它适用于所需的模块? - JavaScript Intellisense with Visual Studio Code: How to get it to work for required modules? 如何在Visual Studio 2015中为Node.js启用智能感知? - How to enable intellisense for nodejs in visual studio 2015? Angular无法在我的NodeJS应用程序中工作(使用Visual Studio 2015)? - Can't get Angular to work in my NodeJS application (using visual studio 2015)? Visual Studio Code 中的异步智能感知节点JS - Asynchronus Intellisense in Visual Studio Code | NodeJS Visual Studio代码IntelliSense无法与NodeJS一起使用 - Visual studio code IntelliSense not working with NodeJS Visual Studio Code Typescript Intellisense停止工作 - Visual Studio Code Typescript Intellisense stops working Angular2 / NodeJS:无法从本地主机上的后端获取数据 - Angular2/NodeJS: Can not get data from the backend on localhost 对于angular2,nodejs是必需的,我可以使用apache服务器运行吗? - Is nodejs mandatory for angular2, can I run with apache server? 使用JavaScript的Visual Studio代码以及TypeScript文件作为Intellisense的参考 - Visual Studio Code using JavaScript with a TypeScript File as Reference for Intellisense
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM