2016-11-17 329 views
10

我想用新的TS2和@types注册表使用Angular 1.x,但遇到问题。它看起来像@types不使用类型注册表所称的“全局”类型。我遇到以下错误:Angular 1.x with TypeScript 2.x,@types和SystemJS - 使用全局打印

error TS2686: 'angular' refers to a UMD global, but the current file is a module. Consider adding an import instead. 

角代码如下:

import "angular"; 
import "angular-route"; 

const app = angular.module("charter-app", ["ui.bootstrap", "ui.router", "templates", "charter-app.controllers"]); 

tsconfig.json:

{ 
    "compilerOptions": { 
    "declaration": false, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "mapRoot": "./", 
    "module": "system", 
    "moduleResolution": "node", 
    "sourceMap": true, 
    "target": "es5", 
    "inlineSources": true, 
    "removeComments": false, 
    "noImplicitAny": false, 
    "typeRoots": [ "node_modules/@types/" ], 
    "types": [ "angular", "angular-ui-bootstrap", "angular-ui-router" ], 
    "outFile": "app.js" 
    } 
} 

尝试使用以下

"devDependencies": { 
    "@types/angular": "^1.5.20", 
    "@types/angular-ui-bootstrap": "^0.13.35", 
    "@types/angular-ui-router": "^1.1.34", 

我正在使用gulp-typescrip pt来编译。我错过了什么?我可以不使用@types库来达到这个目的吗?

回答

1

我正在为Webstorm IDE构建一个文件观察器并且遇到了这个问题。为了解决这个问题,我不得不删除@types/angular,在我的汇编中加入typings angular还包括typings angular加入选项: --types {your file dir}/typings/index.d.ts。同样,你可以将它添加到你的tsconfig.json

我知道这不是你想要的解决方案,但它让我过了那个驼峰。

2

此导入添加到您的代码

import * as angular from "angularjs"; 

Typescript Angular Import因为我可以在这里想到的2种方法的详细信息

+0

我最终放弃了打字。我试着这个语法,但仍然有错误。 –

+0

尝试使用Lodash执行此操作时出现以下错误: System.import中的错误,无法引导。错误:(SystemJS)XHR误差(404未找到)加载的http://本地主机:3000/lodash \t错误:XHR错误(404未找到)加载的http://本地主机:3000/lodash \t错误加载HTTP:/ /172.16.95.136:3000/lodash从http:// localhost:3000/proj/src/app/app(匿名函数)@ localhost /:52 –

26

1)将角标记为global(要容易得多迁移选项)

创建d.ts文件说overrides.d.ts做:

declare global { 
    const angular: ng.IAngularStatic; 
} 
export {}; 

import * as _angular_ from 'angular'; 

declare global { 
    const angular: typeof _angular_; 
} 

,就是这样,没有必要import或管理脚本加载分别为这个。

2)将其导入到每个受影响的文件中。(可能是一个大现有的项目比较繁琐的迁移选项)

如果你可以继续更新所有受影响的文件(通常是有点难以接受,其中已经有很多错误的很多大项目这一做法与此相关的),在任何你参考angular,导入:

import * as angular from "angular"; 

如果你采用这种方法,打字稿将angular transpile作为该文件的依赖,你的模块加载程序(如:SystemJS)需要处理导入。这可以通过让systemJS通过地图/路径管理导入来完成,或者如果脚本通过script标签加载,则可以通过使用SystemJS.registerDynamic or SystemJS.set apis来创建angular的假模块。

+2

作为“lodash”您是正确的。 10x为这个答案你解决了我的问题 – AngularOne

+1

这sovled我试图导入fabricjs的问题。谢谢 – cgatian

+0

@PSL在哪里把文件的方法1)? – eXavier