使用 Webpack 导入节点模块

Import node module with Webpack

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

我需要更改什么才能消除错误?

当你想通过angular-cli使用第三方脚本时,你需要:

安装软件包: npm 安装 base-64

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

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

之后你需要检查安装的包是否有导出模块在打字稿中使用,如:

/**
 * 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);
  }
}