无法找到命名空间' ng'

时间:2017-03-21 21:14:00

标签: javascript angularjs typescript visual-studio-code electron

我正在将简单的angular 1.6项目重写为打字稿。我已经声明了typings依赖项,它们已经编译和安装。尽管有编译错误"找不到命名空间''"在我的服务中。 角度名称也无法识别。

Screenshot

tsconfig.json

{
    "files": [
        "app/**/*.ts",
        "app/**/*.js",
        "main.js",
        "renderer.js"
    ],
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "declaration": false,
        "noImplicitAny": false,
        "allowJs": false,
        "rootDir": ".",
        "typeRoots": ["./typings"] 
    }
}

typings.json

{
  "dependencies": {
    "angular": "registry:dt/angular#1.6.0+20170321201455"
  },
  "globalDependencies": {
    "jquery": "registry:dt/jquery#1.10.0+20170310222111"
  }
}

service.ts

module Services {

    export interface IMyService {
    }

    export class MyService {

        http: ng.IHttpService;
        location: ng.ILocationService;

        constructor($http: ng.IHttpService, $location: ng.ILocationService) {
            this.http = $http;
            this.location = $location;
        }

    }
}

2 个答案:

答案 0 :(得分:4)

也许您应该将angular类型添加到tsconfig.json,如下所示:

{
    ...
    "compilerOptions":
        ...
        "types": [
            "angular"
        ],
}

如果这不起作用,因为不推荐使用typings,我建议您使用npm安装@types/angular

npm install --save-dev @types/angular

并将其包含在上面。

干杯。

答案 1 :(得分:4)

package npm下面添加。

npm install --save-dev @types/angular

然后在.ts文件的顶部包含以下行:

import * as angular from "angular"

它将解决错误。感谢。