typescript import * as angular from 'angular' in app.module.ts 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42193778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:15:38  来源:igfitidea点击:

import * as angular from 'angular' inside app.module.ts file

angulartypescriptsystemjs

提问by AngularOne

I have upgrade angular1 app to bootstrap with angular2 (upgrade option), but after importing angular with systemJS config:

我已将 angular1 应用程序升级为使用 angular2(升级选项)进行引导,但是在使用 systemJS 配置导入 angular 之后:

map: {... 'angular': 'npm:angular/angular.js' ... }

The browser send error:

浏览器发送错误:

systemjs.import error: Error: (SystemJS) angular.module is not a function
    TypeError: angular.module is not a function
        at execute (http://msoqa05.devlab.ad:8088/home/bl/app.module.js:88:21)
        at ZoneDelegate.invoke (https://unpkg.com/[email protected]?main=browser:242:26)
        at Zone.run (https://unpkg.com/[email protected]?main=browser:113:43)
        at https://unpkg.com/[email protected]?main=browser:520:57
        at ZoneDelegate.invokeTask (https://unpkg.com/[email protected]?main=browser:275:35)
        at Zone.runTask (https://unpkg.com/[email protected]?main=browser:151:47)
        at drainMicroTaskQueue (https://unpkg.com/[email protected]?main=browser:418:35)
        at XMLHttpRequest.ZoneTask.invoke (https://unpkg.com/[email protected]?main=browser:349:25)
    Error loading http://msoqa05.devlab.ad:8088/home/bl/app.module.js

Maybe I did not understand it, but in order to import angular I need to mapped it inside systemjs.config? Why?

也许我不明白,但为了导入 angular,我需要将它映射到 systemjs.config 中?为什么?

I am trying to downgrade angular2 componenet inside app.module.ts:

我正在尝试降级 app.module.ts 中的 angular2 组件:

import * as angular from 'angular'; 
import { downgradeComponent } from '@angular/upgrade/static';

angular.module('myApp')
    .directive(
        'helloWorld',
        downgradeComponent({component: HelloWorldComponent}) as angular.IDirectiveFactory
    );

The ts compile is fine with that, but the browser not ok with it and I get this error. angular.module is the basic of angularJS?

ts 编译很好,但浏览器不同意,我收到这个错误。angular.module 是 angularJS 的基础吗?

回答by AngularOne

Doing so solved the problem: no need to import angular with systemjs.

这样做解决了问题:无需使用 systemjs 导入 angular。

import * as _angular_ from 'angular';

declare global {
  const angular: typeof _angular_;
}