简体   繁体   English

IntelliJ和Angular2 / TypeScript-错误TS2307:找不到模块“ angular2 /…”

[英]IntelliJ & Angular2 / TypeScript - error TS2307: Cannot find module 'angular2/…'

There seems to be alot of questions and answers about this problem but so far most answers I have found have been for Windows / Visual Studio. 关于这个问题似乎有很多问题和答案,但是到目前为止,我发现的大多数答案都是针对Windows / Visual Studio的。 I'm using OSX and IntelliJ for a Angular2 with TypeScript project I am working on and I am currently getting warnings / errors (those little red squiggly lines) under the "from" locations when I import from the angular2 library, for example in my app.ts I have: 我正在使用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';

All 3 import statements have warnings under the from locations (where we have "angular2/...."). 所有3个import语句在from位置(我们有“ angular2 / ....”)下都有警告。 I tried to correct this by editiing the typescript-compiler.xml in Intellij: 我试图通过在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>

All other modules I import are fine (like the components I write myself) but those I import from Angular2 have the warning? 我导入的所有其他模块都很好(例如我自己编写的组件),但是我从Angular2导入的模块有警告吗? Has anyone managed to over come this error / problem as I've been trying for a while now to remove the problem. 有没有人设法克服这个错误/问题,因为我已经尝试了一段时间以消除问题。 I currently have Version 1.7.5 of TypeScript installed. 我目前安装了1.7.5版的TypeScript。

Please note that I have no typings.json file or npm typings installed in my dev-dependancies . 请注意,我的dev-dependancies没有安装typings.json文件或npm typings

This is my tsconfig.json file: 这是我的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
  }
}

and this is my tslint.json file: 这是我的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"
    ]
  }
}

For me it was fixed by using relative paths, ie (depending on the location of your .ts file): 对我来说,它是通过使用相对路径(即,取决于您的.ts文件的位置)修复的:

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

Also, in your Intellij Settings make sure to enable "Use tsconfig.json". 此外,请确保在Intellij设置中启用“使用tsconfig.json”。 My settings file looks like this: 我的设置文件如下所示:

<?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>

Please also note thate since a few hours angular2 rc.1 was released ( https://github.com/angular/angular/blob/3229bf16656749427d1def38ced0e1d1d2ae1fa4/CHANGELOG.md ) 另请注意,自从几个小时以来, angular2 rc.1已发布https://github.com/angular/angular/blob/3229bf16656749427d1def38ced0e1d1d2ae1fa4/CHANGELOG.md

This introduces some breaking changes regarding how to import modules (eg "angular/core" gets "@angular/core"). 这引入了一些有关如何导入模块的重大更改(例如,“ angular / core”获取“ @ angular / core”)。

You don't need the ///<reference path=".."/> stuff anymore if you are using the rc version. 如果您使用的是rc版本,则不再需要///<reference path=".."/>东西。 System.js automatically finds the corresponding index.d.ts in the node_modules directory (if set up correctly: https://github.com/angular/quickstart/blob/master/systemjs.config.js ) System.js自动查找对应index.d.ts在node_modules目录(如果正确设置: https://github.com/angular/quickstart/blob/master/systemjs.config.js

I also had to change the target to ES6 otherwise I got error messages that eg Map or Promise was undefined. 我还必须将目标更改为ES6否则会收到错误消息,例如未定义MapPromise If you still want to keep the ES5 target you need to include es6-shim (.js and .d.ts) 如果仍然要保留ES5目标,则需要包含es6-shim (.js和.d.ts)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM