简体   繁体   English

Angular2:找不到命名空间'环境'

[英]Angular2: Cannot find namespace 'environment'

I have configured my tsconfig.json so I can use short import paths in my code for brevity. 我已经配置了我的tsconfig.json所以我可以在我的代码中使用简短的导入路径以简化。 For example, I can do import { FooService } from 'core' instead of being forced to do something like import { FooService } from '../../../core/services/foo/foo.service' . 例如,我可以import { FooService } from 'core'而不是import { FooService } from '../../../core/services/foo/foo.service'强制执行import { FooService } from '../../../core/services/foo/foo.service'

This works just fine, except for some reason now that I have created a new file, one of the imports is throwing an error: 这工作得很好,除了某些原因我现在已经创建了一个新文件,其中一个导入错误:

Cannot find namespace 'environment'. 找不到命名空间'环境'。

This error occurs because of the following line: import { environment } from 'environment'; 由于以下行发生此错误: import { environment } from 'environment';

It's very strange because I have that same import in many other services and none of them throw the error, it only happens in this new file that I created. 这很奇怪,因为我在许多其他服务中都有相同的导入,并且没有一个抛出错误,它只发生在我创建的这个新文件中。

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

As you can see, the paths are set up properly. 如您所见, paths设置正确。 And if they weren't it wouldn't have worked all this time in the other files... 如果它们不是,它将无法在其他文件中一直工作......

What is going on here? 这里发生了什么? Anyone know how to fix this? 有人知道怎么修这个东西吗?

PROBLEM SOLVED 问题解决了

I had a typo in my code which caused this. 我的代码中有一个拼写错误导致了这个问题。 Very hard to find because the intellisense wasn't highlighting it. 很难找到,因为intellisense没有强调它。

I had: url: environment.serverUrl; 我有: url: environment.serverUrl;

When I should've had: url: string = environment.serverUrl; 当我应该有: url: string = environment.serverUrl;

I was using a value as the type accidentally. 我偶然使用了一个值作为类型。 Oops! 哎呀!

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

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