Angular2 Typescript文件的重复标识符

Jon*_*gel 12 typescript angular

在Angular2 TS快速入门之后,我最终在我的项目中的许多文件夹中都有重复文件.

对于浏览器:

typings/browser 
node_modules/angular2/typings/browser
Run Code Online (Sandbox Code Playgroud)

对于es6-shim:

node_modules/angular2/typings/es6-shim
typings/browser/ambient/es6-shim
typings/main/ambient/es6-shim
Run Code Online (Sandbox Code Playgroud)

它会在构建期间导致重复标识符错误.

我们如何防止/抑制TS引发重复的标识符错误?

我在我的排除列表中包含了node_modules,但是,因为我在我的包中使用了Angular2,所以TSD将它们包括在内,因为moduleResolution是"node".用另一个moduleResolution值替换它,例如"classic"会导致其他问题.

这是我的tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "outDir": "./dist"
  },
  "exclude": [
    "bower_components",
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

更新1

这是我的appcomponent.ts:

///<reference path="../../node_modules/angular2/typings/browser.d.ts"/> 
import {bootstrap} from 'angular2/platform/browser';
import {LocationComponent} from '../location/components/locationcomponent';
import {VideosComponent} from '../videos/components/videoscomponent';

bootstrap(LocationComponent, [])
  .catch(err => console.error(err));

bootstrap(VideosComponent, [])
  .catch(err => console.error(err));
Run Code Online (Sandbox Code Playgroud)

更新2

这就是我的web项目文件.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
  <PropertyGroup Label="Globals">
    <ProjectGuid>3775534b-d08c-45f2-8d5a-4a4f6e91edb9</ProjectGuid>
    <RootNamespace>MyProject</RootNamespace>
    <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
    <OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>
  <PropertyGroup>
    <SchemaVersion>2.0</SchemaVersion>
  </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptJSXEmit>None</TypeScriptJSXEmit>
    <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
    <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny>
    <TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
    <TypeScriptRemoveComments>False</TypeScriptRemoveComments>
    <TypeScriptOutFile />
    <TypeScriptModuleResolution>NodeJs</TypeScriptModuleResolution>
    <TypeScriptOutDir />
    <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
    <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
    <TypeScriptSourceMap>True</TypeScriptSourceMap>
    <TypeScriptMapRoot />
    <TypeScriptSourceRoot />
    <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
  </PropertyGroup>
  <Target Name="FixTsBuildConfiguration" BeforeTargets="CompileTypeScript" >
    <PropertyGroup>
      <TypeScriptBuildConfigurations>$(TypeScriptBuildConfigurations.Replace("--moduleResolution NodeJs", "--moduleResolution node"))</TypeScriptBuildConfigurations>
   </PropertyGroup>
  </Target>
  <ItemGroup>
    <DnxInvisibleContent Include="bower.json" />
    <DnxInvisibleContent Include=".bowerrc" />
    <DnxInvisibleContent Include="package.json" />
    <DnxInvisibleFolder Include="wwwroot\bower_components\" />
    <DnxInvisibleFolder Include="wwwroot\node_modules\" />
  </ItemGroup>
  <Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
Run Code Online (Sandbox Code Playgroud)

更新3

我发现在Visual Studio 2015中设置Angular2需要另一种方法.我使用Visual Studio 2015遵循使用TypeScript在ASP.NET 5启动Angular 2中的步骤,我不再遇到任何构建问题.

Din*_*tro 8

打字稿<= 1.6

你需要排除node_modules和分型/主文件在你tsconfig.json**

"exclude": [
    "bower_components/**",
    "node_modules/**",
    "typings/main.d.ts",
    "typings/main/**",
],
Run Code Online (Sandbox Code Playgroud)

没有**它搜索名为node_modules typings/main的文件而不是目录本身.

打字稿> 1.6

在1.6以上的打字稿版本中,**不需要.

编辑

删除你的参考路径appcomponent.ts.编译这样的打字稿时,您不需要参考路径.


Jon*_*gel 0

我发现在 Visual Studio 2015 中设置 Angular2 需要另一种方法。我按照使用 Visual Studio 2015 在 ASP.NET 5 中使用 TypeScript 启动 Angular 2中的步骤进行操作,并且不再遇到任何构建问题。