網站搜尋

javascript - TypeScript:找不到命名空間 'ng'


我正在嘗試使用 tsconfig.json,以便可以避免在許多文件的頂部使用。但我一直收到這個錯誤:

[ts] 找不到命名空間 'ng'。 any

我的 tsconfig.json 文件看起來是這樣的:

{
    "compileOnSave": true,
    "compilerOptions": {
        "noImplicitAny": false,
        "target": "es2015",
        "allowSyntheticDefaultImports": true
    },
    "files": [
        "./typings/index.d.ts"
    ]
}

如你所見,我將它指向我的 index.d.ts 文件,看起來是這樣的:

/// <reference path="globals/angular-ui-router/index.d.ts" />
/// <reference path="globals/angular/index.d.ts" />
/// <reference path="globals/jquery/index.d.ts" />

不過,如果我把這個引用放在我的模組頂部:

app.controller.js

/// <reference path="../../../../typings/index.d.ts" />

...

它能識別 ng 並給我它的類型。為什麼這可能不會工作呢?

任何幫助都非常感激。提前感謝!

答案#1

app.controller.js

您的檔案具有 .js 擴展名,因此不屬於符合 tsconfig.json 的檔案範圍。

快速解決方案

重新命名為 .ts 並在你的 tsconfig.json 中列出該檔案