带有TypeScript 2.x,@ types和SystemJS的Angular 1.x-使用全局类型 [英] Angular 1.x with TypeScript 2.x, @types, and SystemJS - Using global typings

查看:116
本文介绍了带有TypeScript 2.x,@ types和SystemJS的Angular 1.x-使用全局类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Angular 1.x与新的TS2和@types注册表一起使用,但是遇到了问题.看起来@types没有使用类型注册中心称为全局"类型的内容.我遇到以下错误:

I am trying to use Angular 1.x with the new TS2 and @types registry but running into problems. It looks like @types does not use what the typings registry refered to as "global" typings. I am running into the following error:

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:

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-typescript进行编译.我想念什么?我不能为此目的使用@types库吗?

I'm using gulp-typescript to do the compilation. What am I missing? Can I not use the @types libraries for this purpose?

推荐答案

我在这里可以想到2种方法.

I can think of 2 approaches here.

1)将angular标记为全局 . (更容易的迁移选项)

创建一个d.ts文件,例如overrides.d.ts并执行:

Create a d.ts file say overrides.d.ts and do:

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

import * as _angular_ from 'angular';

declare global {
  const angular: typeof _angular_;
}

就是这样,不需要import或为此单独管理脚本加载.

and that's it, no need to import or manage script loading separately for this.

2)将其导入每个受影响的文件中. (在大型现有项目中可能是更乏味的迁移选项)

如果您可以继续更新所有受影响的文件(通常在涉及到很多错误的大型项目中很难采用这种方法),无论您在何处引用angular,都将其导入:

If you can go ahead update all the impacted files (Usually a bit hard to take this approach in a much big project which already has lots of error related to this) where ever you refer to angular, import it:

import * as angular from "angular";

如果采用这种方法,打字稿将以angular作为文件的依赖项进行翻译,并且模块加载器(例如:SystemJS)需要处理导入.这可以通过让systemJS通过地图/路径管理导入来完成,或者如果脚本由script标签加载,则可以使用

If you are taking this approach, typescript will transpile with angular as a dependency for the file and your module loader (eg: SystemJS) needs to handle the import. This may be done either by having systemJS manage the imports via maps/paths or if the script is loaded by script tags then you can create a fake module for angular by using SystemJS.registerDynamic or SystemJS.set apis.

这篇关于带有TypeScript 2.x,@ types和SystemJS的Angular 1.x-使用全局类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆