使用Webpack导入节点模块

时间:2017-08-19 10:34:20

标签: angular typescript webpack webstorm angular-cli

Image

WebStorm告诉我它无法解析文件' base-64'这是我最近通过npm install安装的节点模块。但是,我的应用程序中没有出现错误,我可以毫无问题地使用base64变量。

我需要更改哪些内容才能删除错误?

  • Angular / cli 1.1.3
  • webpack 2.4.0

1 个答案:

答案 0 :(得分:2)

当您想使用带有angular-cli的第三方脚本时,您需要:

安装de package: npm install base-64

.angular-cli.json 脚本中导入包:

 ...,
 "scripts": [
    "../node_modules/base-64/base64.js"
 ],
 ...

之后,您需要检查已安装的软件包是否具有要在typescript中使用的导出模块,如下所示: @angular

/**
 * The module that includes all the basic Angular directives like {@link NgIf}, {@link NgForOf}, ...
 *
 * @stable
 */
export declare class CommonModule {
}

在这种情况下,对于base64你没有模块,那么你可以使用:

import { Component, OnInit } from '@angular/core';

declare let base64: any;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';

  ngOnInit() { 
    console.log(base64);
  }
}
相关问题