【问题标题】:IntelliJ & Angular2 / TypeScript - error TS2307: Cannot find module 'angular2/...'IntelliJ & Angular2 / TypeScript - 错误 TS2307:找不到模块 'angular2/...'
【发布时间】:2016-05-04 19:40:11
【问题描述】:

似乎有很多关于这个问题的问题和答案,但到目前为止,我发现的大多数答案都是针对 Windows / Visual Studio 的。我正在将 OSX 和 IntelliJ 用于我正在处理的带有 TypeScript 项目的 Angular2,当我从 angular2 库导入时,我目前在“来自”位置下收到警告/错误(那些小的红色波浪线),例如在我的app.ts 我有:

/// <reference path="node_modules/angular2/ts/typings/node/node.d.ts"/>
/// <reference path="node_modules/angular2/typings/browser.d.ts"/>

import { bootstrap } from "angular2/platform/browser";
import { Component } from "angular2/core";
import { ROUTER_DIRECTIVES, ROUTER_PROVIDERS, RouteConfig} from 'angular2/router';

所有 3 个导入语句在 from 位置(我们有“angular2/....”的位置)下都有警告。我试图通过在 Intellij 中编辑 typescript-compiler.xml 来纠正这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="TypeScriptCompiler">
    <option name="isCompilerEnabled" value="true" />
    <option name="typeScriptServiceDirectory" value="$USER_HOME$/node_modules/typescript/bin" />
    <option name="typeScriptCompilerParams" value="-m amd -t ES5" />
    <option name="versionType" value="SERVICE_DIRECTORY" />
  </component>
</project>

我导入的所有其他模块都很好(比如我自己编写的组件)但是我从 Angular2 导入的那些有警告?有没有人设法克服这个错误/问题,因为我已经尝试了一段时间来解决这个问题。我目前安装了 TypeScript 1.7.5 版。

请注意,我的dev-dependancies 中没有安装typings.json 文件或npm typings

这是我的 tsconfig.json 文件:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "files": [
    // there is an array of files here
  ],
  "filesGlob": [
    "app/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "atom": {
    "rewriteTsconfig": true
  }
}

这是我的 tslint.json 文件:

{
  "rules": {
   "align": [true,
        "parameters",
        "arguments",
        "statements"],
    "class-name": true,
    "comment-format": [true, "check-space"],
    "curly": true,
    "eofline": true,
    "forin": true,
    "indent": [true, "spaces"],
    "interface-name": true,
    "label-position": true,
    "label-undefined": true,
    "jsdoc-format": true,
    "max-line-length": [true, 80],
    "member-ordering": [true,
        "public-before-private",
        "static-before-instance",
        "variables-before-functions"
    ],
    "no-arg": true,
    "no-bitwise": true,
    "no-console": [true,
      "debug",
      "info",
      "time",
      "timeEnd",
      "trace"
    ],
    "no-construct": true,
    "no-debugger": true,
    "no-duplicate-key": true,
    "no-duplicate-variable": true,
    "no-empty": true,
    "no-eval": true,
    "no-require-imports": true,
    "no-shadowed-variable": true,
    "no-string-literal": true,
    "no-switch-case-fall-through": true,
    "no-trailing-comma": true,
    "no-trailing-whitespace": true,
    "no-unused-expression": true,
    "no-unused-variable": true,
    "no-unreachable": true,
    "no-use-before-declare": true,
    "no-var-keyword": true,
    "one-line": [true,
      "check-open-brace",
      "check-catch",
      "check-else",
      "check-whitespace"
    ],
    "quotemark": [true, "single"],
    "radix": true,
    "semicolon": true,
    "sort-object-literal-keys": false,
    "triple-equals": [true, "allow-null-check"],
    "typedef": [true,
        "call-signature",
        "parameter",
        "property-declaration",
        "variable-declaration",
        "member-variable-declaration"
    ],
    "typedef-whitespace": [true, {
        "call-signature": "nospace",
        "index-signature": "nospace",
        "parameter": "nospace",
        "property-declaration": "nospace",
        "variable-declaration": "nospace"
    }],
    "variable-name": [true,
      "allow-leading-underscore"],
    "whitespace": [true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-separator",
      "check-type"
    ]
  }
}

【问题讨论】:

  • 您使用的是哪个版本的 Angular 2?
  • "angular2": "2.0.0-beta.16"
  • 升级到 beta.17 没有区别

标签: intellij-idea typescript angular


【解决方案1】:

对我来说,它是通过使用相对路径来修复的,即(取决于您的 .ts 文件的位置):

///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>

此外,请确保在您的 Intellij 设置中启用“使用 tsconfig.json”。 我的设置文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="TypeScriptCompiler">
    <option name="isCompilerEnabled" value="true" />
    <option name="typeScriptCompilerParams" value="--module &quot;amd&quot; --target &quot;ES5&quot; --experimentalDecorators" />
    <option name="useConfig" value="true" />
  </component>
</project>

还请注意,angular2 rc.1 已经发布了几个小时 (https://github.com/angular/angular/blob/3229bf16656749427d1def38ced0e1d1d2ae1fa4/CHANGELOG.md)

这引入了一些关于如何导入模块的重大更改(例如“angular/core”获取“@angular/core”)。

如果您使用的是 rc 版本,则不再需要 ///&lt;reference path=".."/&gt; 的东西。 System.js会自动在node_modules目录下找到对应的index.d.ts(如果设置正确的话:https://github.com/angular/quickstart/blob/master/systemjs.config.js

我还必须将目标更改为ES6,否则我会收到错误消息,例如MapPromise 未定义。如果您仍想保留 ES5 目标,则需要包含 es6-shim(.js 和 .d.ts)

【讨论】:

    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2016-06-12
    • 2016-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2017-09-10
    • 2017-12-10
    相关资源
    最近更新 更多