【问题标题】:Import node module with Webpack使用 Webpack 导入节点模块
【发布时间】:2018-01-27 22:57:51
【问题描述】:

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

我需要改变什么来消除错误?

  • Angular/cli 1.1.3
  • webpack 2.4.0

【问题讨论】:

    标签: angular typescript webpack webstorm angular-cli


    【解决方案1】:

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

    安装de包: npm install 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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 2013-08-25
      • 2018-08-07
      • 1970-01-01
      • 2019-05-09
      • 2018-03-15
      • 1970-01-01
      相关资源
      最近更新 更多