Webpack 2中的动态导入不起作用

维恩·阮(Vien Nguyen)

我想在webpack 2中使用动态导入。我使用webpack 2.4.1

index.js

import 'babel-polyfill'
import React from 'react'
import { render } from 'react-dom'
import Root from './containers/Root'
import '../styles/custom.scss'

var btn = document.getElementsById('button');
btn.addEventListener("click", () => {
    import('./assets/chunk.css').then(()=>{
        console.log("Success")
    })
});

render(
  <Root />,
  document.getElementById('root')
)

但是当我运行“ npm start”时,它不起作用,并在index.js的第9行通知错误

Module build failed: SyntaxError: 'import' and 'export' may only appear at the top level (9:4)

谢谢你的帮助!

乔菲

正如无数迭加所提到的,答案是

  1. 安装 babel-plugin-syntax-dynamic-import
  2. 确保'syntax-dynamic-import'出现在您的通天塔中options.plugins

babel.rc

{
  "plugins": [
    "syntax-dynamic-import",
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章