简体   繁体   English

在Angular中导入外部模块功能

[英]Importing external module functions in Angular

I wrote an es6 module, with some exported functions, and uploaded it to npm (uplaoded its transpiled es5 code). 我编写了一个es6模块,其中包含一些导出的功能,并将其上传到npm(更新了已编译的es5代码)。

I'm trying to consume this package in an Angular project that uses SystemJS as a module loader (it's an hybrid app of AngularJS 1.5 and Angular 5.2, in the middle of a migration process). 我试图在一个使用SystemJS作为模块加载器的Angular项目中使用此程序包(在迁移过程中,它是AngularJS 1.5和Angular 5.2的混合应用程序)。

I added to systemjs.config.js the new library, similar to how I added rxjs (directing it to its path under node_modules). 我向systemjs.config.js添加了新库,类似于我添加rxjs(将其定向到node_modules下的路径)的方式。

But when trying to use one of the module's public functions, IntelliJ doesn't know where to import it from (as it knows for example when using noop function of rxjs), and if I add the import statement manually (import { func-name } from 'package-name'), then TypeScript compiler throws an error. 但是,当尝试使用模块的公共功能之一时,IntelliJ不知道从何处导入(例如,在使用rxjs的noop函数时就知道),并且是否手动添加了import语句(import {func-name },然后TypeScript编译器将引发错误。

Any suggestions? 有什么建议么? Do I need to change something in the module I published to npm, or in the way I'm trying to consume the module functions in Angular? 我需要在发布到npm的模块中进行某些更改,还是尝试使用Angular中的模块功能进行更改?

Thanks. 谢谢。

Angular code 角度代码

systemjs.config.js: systemjs.config.js:

map: {
      'app': './',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',

      // other libraries
      'rxjs'                     : 'npm:rxjs',
      .
      .
      'my-package'               : 'npm:my-package/dist/my-package.js'
    },

my-component.component.ts: my-component.component.ts:

import { func } from 'my-package';
.
.
.
ngAfterContentInit() {
    func();
}

TypeScript compiler error: TypeScript编译器错误:

error TS2307: Cannot find module 'package-name'

It works now, after I changed the npm package to build a umd version. 在我更改了npm软件包以构建umd版本之后,它现在可以工作了。

And in systemjs.config.js: 在systemjs.config.js中:

'my-package': 'npm:my-package/dist/my-package.umd.js'

In addition, generating an index.d.ts file for my npm package (TypeScript Definition File), made IntelliJ to do automatic imports for the library's functions, and made tsc not to fail with "Cannot find module" error. 另外,为我的npm包生成一个index.d.ts文件(TypeScript定义文件),使IntelliJ自动导入该库的功能,并使tsc不会因“找不到模块”错误而失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM