首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IntelliJ & Angular2 / TypeScript - error TS2307:无法找到模块‘angular2 2/.’

IntelliJ & Angular2 / TypeScript - error TS2307:无法找到模块‘angular2 2/.’
EN

Stack Overflow用户
提问于 2016-05-03 13:45:01
回答 1查看 4.5K关注 0票数 2

关于这个问题,似乎有很多问题和答案,但到目前为止,我发现的大多数答案都是针对Windows / Visual的。我正在使用OSX和IntelliJ进行Angular2 with TypeScript项目,并且当我从angular2库导入时,当前在" from“位置下收到警告/错误(那些红色的小行),例如在我的app.ts中:

代码语言:javascript
复制
/// <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个import语句都在from位置(我们有“angular2/.”)下有警告。我试图在Intellij中通过editiing类型- tried piler.xml来纠正这一点:

代码语言:javascript
复制
<?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导入的模块有警告吗?是否有人设法克服了这个错误/问题,因为我已经尝试了一段时间来消除这个问题。我现在已经安装了1.7.5版本的TypeScript。

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

这是我的tsconfig.json文件:

代码语言:javascript
复制
{
  "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文件:

代码语言:javascript
复制
{
  "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"
    ]
  }
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-04 09:22:36

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

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

另外,在Intellij设置中,请确保启用"Use tsconfig.json“。我的设置文件如下所示:

代码语言:javascript
复制
<?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)

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

如果您正在使用rc版本,那么您不再需要///<reference path=".."/>了。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)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37005999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档