简体   繁体   English

如何使用ES6导入插件? (获取ReferenceError:未定义插件)

[英]How to import a plugin using ES6? (Getting ReferenceError: Plugin is not defined)

Trying to wrap my head around Webpack. 试图把我的头围绕在Webpack上。 I was able to set it up and get it working (combines my scripts and styles just fine), but I'm having trouble with a lazy loading plugin called bLazy, which I installed via npm. 我能够设置它并使其工作(将我的脚本和样式组合得很好),但是我遇到了一个名为bLazy的延迟加载插件,我通过npm安装。 Here's the code in the file that I've defined as the entry point: 这是我定义为入口点的文件中的代码:

// Stylesheets

import './style.scss';

// Scripts

import 'salvattore'; 
import 'blazy'; 

var bLazy = new Blazy(); // This is the documented way to initialize bLazy.

I am getting the error: Uncaught ReferenceError: Blazy is not defined . 我收到错误: Uncaught ReferenceError: Blazy is not defined The Salvattore plugin, which is self-initializing, works fine. Salvatore插件,自我初始化,工作正常。 I'm also using jQuery, but bLazy is written in Javascript, so there shouldn't be any conflicting issues. 我也使用jQuery,但bLazy是用Javascript编写的,所以不应该有任何冲突的问题。

What am I doing wrong? 我究竟做错了什么?

+++ UPDATE +++ +++ UPDATE +++

I've changed the way I posed my question because apparently it's about ES6 and not Webpack, like I thought it was. 我改变了我提出问题的方式,因为显然它是关于ES6而不是Webpack,就像我认为的那样。

喜欢这个import Blazy from 'blazy'

In the script you want to import you should "export" a value, like this: 在要导入的脚本中,您应该“导出”一个值,如下所示:

class Blazy {...}

export default Blazy;

Then in script where you want to use it, you need to "import" this value: 然后在要使用它的脚本中,您需要“导入”此值:

import Blazy from './blazy'; // Note the path in quotes is relative to your current script file

let blazy = new Blazy();

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

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