环境模块声明不能指定相对模块名称

Posted

技术标签:

【中文标题】环境模块声明不能指定相对模块名称【英文标题】:Ambient module declaration cannot specify relative module name 【发布时间】:2016-09-06 22:10:03 【问题描述】:

我正在使用 Angular 2。我的项目正在运行,但 Visual Studio 中显示以下错误:

typings.json:


       "ambientDependencies": 
        "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd"
    

package.json:


    "name": "asp-net-angular2",
    "version": "1.0.0",
    "author": "TestStack",
    "description": "",
    "scripts": 
        "postinstall": "typings install"
    ,
    "license": "Apache-2.0",
    "dependencies": 
        "@angular/common": "2.0.0-rc.1",
        "@angular/compiler": "2.0.0-rc.1",
        "@angular/core": "2.0.0-rc.1",
        "@angular/http": "2.0.0-rc.1",
        "@angular/platform-browser": "2.0.0-rc.1",
        "@angular/platform-browser-dynamic": "2.0.0-rc.1",
        "@angular/router": "2.0.0-rc.1",
        "@angular/router-deprecated": "2.0.0-rc.1",
        "@angular/upgrade": "2.0.0-rc.1",

        "systemjs": "0.19.27",
        "es6-shim": "^0.35.0",
        "es6-promise": "^3.1.2",
        "reflect-metadata": "^0.1.3",
        "rxjs": "5.0.0-beta.6",
        "zone.js": "^0.6.12",

        "angular2-in-memory-web-api": "0.0.7",
        "bootstrap": "^3.3.6",
        "jquery": "^2.2.3"
    ,
    "devDependencies": 
        "typescript": "^1.8.10",
        "gulp": "^3.9.1",
        "path": "^0.12.7",
        "gulp-clean": "^0.3.2",
        "fs": "^0.0.2",
        "gulp-concat": "^2.6.0",
        "gulp-typescript": "^2.13.1",
        "gulp-tsc": "^1.1.5",
        "del": "^2.2.0",
        "gulp-autoprefixer": "^3.1.0",
        "gulp-cssnano": "^2.0.0",
        "gulp-html-replace": "^1.5.4",
        "gulp-htmlmin": "^1.0.5",
        "gulp-uglify": "^1.5.3",
        "merge-stream": "^1.0.0",
        "systemjs-builder": "^0.15.16",

        "typings": "^0.8.1"
    ,
    "repository":  

tsconfig.json:


  "compileOnSave": true,
  "compilerOptions": 
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "noImplicitAny": false,
    "suppressImplicitAnyIndexErrors": true
  ,
  "exclude": [
    "node_modules",
    "wwwroot",
    "typings/main",
    "typings/main.d.ts"
  ]

和项目设置:

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <TypeScriptTarget>ES5</TypeScriptTarget>
    <TypeScriptJSXEmit>None</TypeScriptJSXEmit>
    <TypeScriptCompileOnSaveEnabled>True</TypeScriptCompileOnSaveEnabled>
    <TypeScriptEmitDecoratorMetadata>True</TypeScriptEmitDecoratorMetadata>
    <TypeScriptNoImplicitAny>True</TypeScriptNoImplicitAny>
    <TypeScriptModuleKind>CommonJS</TypeScriptModuleKind>
    <TypeScriptRemoveComments>False</TypeScriptRemoveComments>
    <TypeScriptExperimentalDecorators>True</TypeScriptExperimentalDecorators>
    <TypeScriptOutFile />
    <TypeScriptOutDir />
    <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations>
    <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError>
    <TypeScriptSourceMap>True</TypeScriptSourceMap>
    <TypeScriptMapRoot />
    <TypeScriptSourceRoot />
  </PropertyGroup>

如何消除这些错误。我尝试过但没有成功。我在.d.ts 文件上的错误:

【问题讨论】:

我可以建议您将 ES5 更改为 ES6,但我仍然想知道您是如何做到这一点的? 在声明模块时不能使用相对路径,它必须是绝对路径,在你的情况下声明模块 ../../Observable 无效且打字稿不允许 【参考方案1】:

我看到你有 tsconfig.json,所以你可能正在使用命令行来构建你的 tsc 文件。

您可以通过在项目设置中包含以下内容来阻止 Visual Studio 中的编译(所有错误都会消失,因为您不是在 VS 中构建),

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>

否则,您可以通过将 ts 文件和 main.d.ts 添加到项目设置中来明确包含这些文件。

<ItemGroup>
   <TypeScriptCompile Include="$(ProjectDir)\src\**\*.ts" />
</ItemGroup>
<ItemGroup>
   <TypeScriptCompile Include="$(ProjectDir)\src\main.d.ts" />
</ItemGroup>

【讨论】:

这对我没有帮助,您还有其他选择吗?

以上是关于环境模块声明不能指定相对模块名称的主要内容,如果未能解决你的问题,请参考以下文章

PYTHON-DAY18-包 相对 绝对导入

模块之相对路径导入

相对导入

如何相对于子目录运行詹金斯管道?

详解Python中的相对导入和绝对导入

Python技术基础知识点:OS模块的应用