简体   繁体   English

为什么我在ES6中导入Javascript无法正常工作? 意外的标记 *

[英]Why is my Javascript import in ES6 not working? Unexpected token *

im currently trying to implement FilePond in my Project. 我目前正在尝试在我的项目中实现FilePond I've tried to implement it through npm installation, but somehow my Browser constantly throws this error: 我试图通过npm安装来实现它,但是以某种方式我的浏览器不断抛出此错误:

Uncaught SyntaxError: Unexpected token *

I'm guessing it has something to with ES6, but I can't figure out how to fix it. 我猜想它与ES6有关,但是我不知道如何解决它。 My Javascript code looks like this: 我的Javascript代码如下所示:

 import * as FilePond from 'filepond'; import FilePondPluginImagePreview from 'filepond-plugin-image-preview'; FilePond.registerPlugin( FilePondPluginImagePreview() ); const inputElement = document.querySelector('input[type="file"]'); const pond = FilePond.create( inputElement,{ allowImagePreview: true, }); FilePond.setOptions({ server: 'test/' }); 

I've tried to google an answer to this but it seems like I can't find a solution to this one. 我试图用谷歌搜索这个问题的答案,但似乎找不到解决方案。 I've red something about compiling the ES6 code with Bable , but don't really know.. 我已经用Bable编译了ES6代码,但并不太清楚。

Thank you for your help! 谢谢您的帮助! :) :)

If you want to use FilePond in the browser AND you want to use the ES module version you can go for dynamic imports or a script tag with type module . 如果要在浏览器中使用FilePond并且要使用ES模块版本,则可以进行动态导入或使用module类型的脚本标记。 Browser support isn't fantastic. 浏览器支持不是很好。

Another option is to use the UMD version of the library and simply include the script tag as described in the docs. 另一个选择是使用库的UMD版本,并仅包含docs中所述的script标签。

The third option would be to use Rollup or Webpack with Babel. 第三种选择是将Baup或Webpack与Babel一起使用。

Dynamic Imports 动态导入

<link href="./filepond.css" rel="stylesheet">
<input type="file"/>

<script>
import('./filepond.esm.js').then(FilePond => {
  FilePond.create(document.querySelector('input'));
});
</script>

Type Module 类型模块

<link href="./filepond.css" rel="stylesheet">
<input type="file"/>

<script type="module" src="./filepond.esm.js"></script>
<script>
document.addEventListener('FilePond:loaded', e => {
    const FilePond = e.detail;
    FilePond.create(document.querySelector('input'));
})
</script>

Some other notes 其他一些注意事项

FilePond.registerPlugin(
    FilePondPluginImagePreview()
);

Remove the () , those are not needed. 删除() ,不需要这些。

const pond = FilePond.create( inputElement,{
    allowImagePreview: true,
});

Plugins are automatically enabled so setting allowImagePreview to true is also not required. 插件会自动启用,因此也不需要将allowImagePreview设置为true

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

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