Angular 2 : Cannot find namespace 'environment'

标签 angular typescript

我已经配置了我的 tsconfig.json 所以我可以在我的代码中使用简短的导入路径来简洁。例如,我可以执行 import { FooService } from 'core' 而不是被迫执行 import { FooService } from '../../../core/services/foo/foo.service'.

这工作得很好,除了出于某种原因,现在我已经创建了一个新文件,其中一个导入抛出错误:

Cannot find namespace 'environment'.

出现此错误是因为以下行:import { environment } from 'environment';

这很奇怪,因为我在许多其他服务中都有相同的导入,但没有一个会抛出错误,它只发生在我创建的这个新文件中。

这是我的tsconfig.json:

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "paths": {
      "core": ["app/core"],
      "core/*": ["app/core/*"],

      "shared": ["app/shared"],
      "shared/*": ["app/shared/*"],

      "modal": ["app/modal"],
      "modal/*": ["app/modal/*"],

      "environment": ["environment/environment"]// HERE
    },
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

如您所见,路径 已正确设置。如果不是这样,它就不会一直在其他文件中工作...

这是怎么回事?有人知道如何解决这个问题吗?

最佳答案

问题已解决

我的代码中有一个拼写错误导致了这个问题。很难找到,因为智能感知没有突出显示它。

我有:url: environment.serverUrl;

当我应该有:url: string = environment.serverUrl;

我不小心使用了一个值作为类型。糟糕!

关于 Angular 2 : Cannot find namespace 'environment' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45034123/

相关文章:

Angular4在div中加载外部html页面

typescript - 如何解压作为 Typescript 联合且可能是数组的返回类型?

angular - 想要扩展空接口(interface)但得到 lint 错误 : no-empty-interface

angular - 在 Angular Universal 中使用 lodash-es 的正确方法是什么?

Angular2 RC2 Expression 检查后发生了变化。

intellij-idea - 如何修复 IntelliJ IDEA 2016 TypeScript 错误, "Cannot start compiler process"

Angular 2 所见即所得的表单生成器

typescript - 有没有办法通过 tslint 规则在 Typescript 类上强制执行方法返回类型?

css - ng-select Angular2面板透明

angular - 如何解决 Ionic2 中的 CORS 问题和 JSONP 问题