ElementUI / babel-plugin-component

Modular element-ui build plugin for babel.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

怎么动态使用babel-plugin-component这个插件

jackieveev opened this issue · comments

如题,我近期用到了elementui,因为觉得很好用,就把我之前项目整个替换成该UI库,但是现在遇到一个问题,希望各位大大给点帮助,因为网络上实在没有找到相关问题。
我的项目里有两个入口文件,index.js和login.js,对应两个html。
因为登录页只用到少许组件,所以我就想在login页使用babel-plugin-component这个插件,但index.js里是照常引入。
import Vue from 'vue';
import ElementUI from 'element-ui';
Vue.use(ElementUI);
这个时候babel-plugin-component就会报错
ERROR in ./src/app.js
Module build failed: Error: C:/Users/kuo/Desktop/vue-projects/gk/src/app.js: [babel-plugin-component] If you are using bothon-demand and importing all, make sure to invoke the importing all first.
at importMethod (C:\Users\kuo\Desktop\vue-projects\gk\node_modules\babel-plugin-component\lib\core.js:103:21)

请问有什么配置或方法是可以动态使用babel-plugin-component这个插件的吗?感激不尽。

I got the error too.

[babel-plugin-component] If you are using bothon-demand and importing all, make sure to invoke the importing all first.

我也碰到了,不过是新增babel-plugin-lodash插件时报的,后来发现是用nuxt hmr时直接修改编译配置会报这个错,停掉服务重新编译就没了

run dev时可以,build时又不行了。。

查了下原因,插件报错位置:https://github.com/ElementUI/babel-plugin-component/blob/master/src/core.js#L85
这里中间少了个空格,应该是using both on-demand and importing all,指全量引入import Element from 'element-ui'和单独引入import { Message } from 'element-ui'都有时,需要把全量引入的代码放在前面


我的情况是nuxt的plugins中引入了axios和element-ui:

  plugins: [
    '~/plugins/element-ui.js',
    '~/plugins/axios.js',
  ],

在axios.js中单独引入了Message,在element-ui.js中是全量引入的。
把element-ui放在了前面,报错了,然后换了下位置:

  plugins: [
    '~/plugins/axios.js',
    '~/plugins/element-ui.js',
  ],

就可以了,怀疑这里的plugin是逆序加载的,真实情况没有深究,知道的同学请告诉我一下。

是不是没人维护这个插件了呀?

我是删除了.babelrc 下的plugins/component解决的。

相同场景:多入口,一个入口全量依赖、一个入口按需加载。
相同问题。

commented

我是删除了.babelrc 下的plugins/component解决的。

我也是

have you tried restart