解决Vue中使用Element UI编译时babel-preset-es2015报错问题

首先我的开发环境是基于webpack4.X,配置好开发环境之后,按照Element UI官方文档配置出现编译错误,详细步骤如下。
webpack4 + Vue 开发环境配置参考

1、安装element-ui

npm i element-ui -S

2、通过按需引入方式

借助 babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的。

首先,安装 babel-plugin-component:

npm install babel-plugin-component -D

然后,手动创建 .babelrc 文件(注意前面的‘ . ’前缀),写入以下配置:

  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

接下来,就是按需引入了,比如 Button 和 Select,那么需要在 main.js入口文件中定义:

import Vue from 'vue';
import { Button, Select } from 'element-ui';
import App from './App.vue';

Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
/* 或写为
 * Vue.use(Button)
 * Vue.use(Select)
 */

new Vue({
  el: '#app',
  render: h => h(App)
});

最后,在App.vue中使用:

<template>
  <div>
    {{ massage }}
    <el-row>
      <el-button>默认按钮1</el-button>
      <el-button type="primary">主要按钮</el-button>
      <el-button type="success">成功按钮</el-button>
      <el-button type="info">信息按钮</el-button>
      <el-button type="warning">警告按钮</el-button>
      <el-button type="danger">危险按钮</el-button>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      massage: "Hello word!"
    };
  }
};
</script>

3、执行npm start之后,报出以下错误

Error: Cannot find module 'babel-preset-es2015' from xxx

一看就知道缺少了babel-preset-es2015,于是安装:

npm install babel-preset-es2015 -D

安装完,重新启动,然后又报错了,意思是插件/预设文件不允许导出对象:

Error: Plugin/Preset files are not allowed to export objects, only functions. In xxx

百度了下,说是因为 babel 的版本冲突,参考babel版本兼容报错处理,反正按照这个我是没解决问题,只好另求他法。

4、终于找到解决问题的办法
根据babel官方介绍,推荐使用更新的babel-preset-env插件:

babel-preset-es2015 -> babel-preset-env

We’re super 😸 excited that you’re trying to use ES2015 syntax, but instead of continuing yearly presets, the team recommends using babel-preset-env. By default, it has the same behavior as previous presets to compile ES2015+ to ES5. Please check out the v1.x readme for more info. (For Babel 7, we have moved the preset into the main babel repo.
Babel 7
If you are using Babel version 7 you will need to run npm install @babel/preset-env and have “presets”: ["@babel/preset-env"] in your configuration.

因此,修改.babelrc文件如下,把es2015改成@babel/preset-env(先安装@babel/preset-env):

  "presets": [["@babel/preset-env", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

最后,npm start,Compiled successfully.

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue使用element-ui的el-dialog对话框并实现拖动功能,可以使用vue-draggable-resizable插件来实现。 1. 安装插件 使用npm或者yarn进行安装。 ``` npm install vue-draggable-resizable --save ``` 或者 ``` yarn add vue-draggable-resizable ``` 2. 引入插件 在Vue组件引入vue-draggable-resizable插件,并注册为全局组件。 ```javascript import Vue from 'vue' import VueDraggableResizable from 'vue-draggable-resizable' import 'vue-draggable-resizable/dist/VueDraggableResizable.css' Vue.component('vue-draggable-resizable', VueDraggableResizable) ``` 3. 使用插件 在el-dialog组件嵌套vue-draggable-resizable组件,并设置对话框的宽度和高度。 ```html <template> <div> <el-button type="primary" @click="dialogVisible = true">打开对话框</el-button> <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"> <vue-draggable-resizable :w="dialogWidth" :h="dialogHeight"> <p>这是一个可以拖动的对话框</p> </vue-draggable-resizable> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, dialogWidth: 400, dialogHeight: 300 } }, methods: { handleClose(done) { this.dialogVisible = false done() } } } </script> ``` 在这个示例,我们将vue-draggable-resizable组件嵌套在el-dialog组件,并设置了对话框的宽度和高度。我们还定义了一个handleClose方法来处理对话框关闭的事件。 现在你可以在Vue使用element-ui的el-dialog对话框并实现拖动功能了。注意:vue-draggable-resizable插件的样式可能需要根据自己的需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值