我正在尝试将Angular 1与Typescript一起使用。我正在使用npm @types来获取我的类型声明文件。 Webstorm识别类型定义,但是当我运行tsc
时,输出如下:
node_modules/@types/angular-material/index.d.ts(6,26): error TS2307: Cannot find module 'angular'.
node_modules/@types/angular-ui-router/index.d.ts(6,26): error TS2307: Cannot find module 'angular'.
src/core.config.ts(9,68): error TS2305: Module 'angular' has no exported member 'ui'.
src/core.config.ts(19,16): error TS2339: Property 'name' does not exist on type '{}'.
src/core.config.ts(37,43): error TS2305: Module 'angular' has no exported member 'ui'.
src/core.controller.ts(6,80): error TS2305: Module 'angular' has no exported member 'material'.
我的tsconfig文件如下所示:
{
"compilerOptions": {
"module": "system",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"outFile": "out.min.js",
"sourceMap": true,
"typeRoots": [
"./node_modules/@types"
],
"types": [
"angular",
"angular-material",
"angular-ui-router"
]
},
"files": ["file.ts"]
}
我尝试为定义文件添加引用路径(/// <reference path="node_modules/@types/angular-material/index.d.ts" />
),但它没有帮助。我觉得我做错了什么但我找不到什么。有人知道我错过了什么吗?
修改的
我发现当我使用我的tsconfig.json文件进行编译时,我只会收到错误。当我使用tsc core.config.ts
时,没有错误,但是在我的tsconfig文件中有文件:[“core.config.ts”]我得到的错误与上面看到的相同。
答案 0 :(得分:3)
更改
// <reference path="node_modules/@types/angular-material/index.d.ts" />
要
/// <reference types="angular" />
/// <reference types="angular-material" />
为您引用的各种模块添加额外的行。
不幸的是,目前还没有官方文档,但它在待办事项清单上。 https://github.com/Microsoft/TypeScript-Handbook/issues/348
答案 1 :(得分:0)
除了模块定义引用(“/// ...”) 您需要导入角度才能使其显示为
/// <reference types="angular" />
/// <reference types="angular-material" />
import * as angular form "angular"
import {material, extend, IPromise, IDeferred} from "angular";
...
angular.extend(...);
extend(); // Note: that is a shortcut to angular.extend; see the second import
private $mdDialog: material.IDialogService;
var service: (value: any): IPromise<any> {...}
...
答案 2 :(得分:0)
我使用打字机,我遇到了同样的问题。 为了解决这个问题,我在typings.json文件中添加了angular-material:
{
"globalDependencies": {
"jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#dc9dabe74a5be62613b17a3605309783a12ff28a",
"angular-ui-router": "registry:dt/angular-ui-router#1.1.5+20160707113237",
"angular": "registry:dt/angular#1.5.0+20160627105203",
"angular-material": "github:DefinitelyTyped/DefinitelyTyped/angular-material/angular-material.d.ts#2d0a1b1502610025bd55ce8eaa48069da8a16c82",
"angular-mocks": "github:DefinitelyTyped/DefinitelyTyped/angularjs/angular-mocks.d.ts#dc9dabe74a5be62613b17a3605309783a12ff28a",
"jquery": "github:DefinitelyTyped/DefinitelyTyped/jquery/jquery.d.ts#dc9dabe74a5be62613b17a3605309783a12ff28a",
"require": "registry:dt/require#2.1.20+20160316155526",
"es6-shim": "registry:dt/es6-shim#0.31.2+20160602141504"
}
}
之后我运行&#39; typings install&#39;,这就是全部。我希望它会对你有所帮助。