element ui 按需引入

1、创建vue脚手架

vue create 项目名称

2、安装element ui

npm i element-ui --save

3、安装 babel-plugin-component

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

npm install babel-plugin-component -D

4、 .babelrc 文件配置

babel.config.js文件中配置

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

5、创建element文件夹

文件路径:

res>element>index.js

推荐用代码分离法,也可在main.js中直接书写

好处:分离main.js文件代码,简化代码结构,让代码更加具可读性

// 导入自己需要的组件
import { Row,Button } from "element-ui";

const element = {
  install: function (Vue) {
    Vue.use(Button);
    Vue.use(Row);
  },
};

export default element;

在main.js中进行调用js文件

import Vue from "vue";
import App from "./App.vue";
import element from "./elementui";
Vue.use(element);

Vue.config.productionTip = false;

new Vue({
  render: (h) => h(App),
}).$mount("#app");

6、组件中使用

<template>
  <div class="home">
    <el-row>
      <el-button>默认按钮</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 {};
</script>

<style></style>

到这里成功了一大半啦~

只是一大半的原因是,样式没有出来:

finally:解决那需引入样式不出现问题

项目中使用的vue/cli脚手架版本为4.x,babel配置文件为babel.config.js

babel.config.js可以视为与.babelrc效果相同

安装依赖:

npm i @babel/preset-env -D

不再使用es2015,改成@babel/preset-env,两者都是用来编译es6语法;

res>element>index.js

代码修改如下:

module.exports = {
  presets: [
    "@vue/cli-plugin-babel/preset", 
    ["@babel/preset-env", { modules: false }]
  ],
  plugins: [
    [
      "component",
      {
        libraryName: "element-ui",
        styleLibraryName: "theme-chalk",
      },
    ],
  ],
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要按引入Vue Element UI组件,你要进行以下几个步骤: 1. 首先,安装Element UI和babel-plugin-component。使用命令`npm i element-ui --save`来安装Element UI,然后使用命令`npm install babel-plugin-component -D`来安装babel-plugin-component插件。 2. 打开项目根目录下的babel.config.js文件,并进行配置。在presets数组中添加`["@babel/preset-env", { "modules": false }]`,在plugins数组中添加以下代码: ```javascript ["component", { libraryName: "element-ui", styleLibraryName: "theme-chalk" }] ``` 这个配置会将Element UI的组件按引入,并引入对应的样式。 3. 新建一个文件`/src/components/plugins/element.js`,并在这个文件中引入Element UI组件。例如,如果你要使用Button、Form、FormItem和Input组件,你可以在element.js文件中写入以下代码: ```javascript import Vue from 'vue' import { Button } from 'element-ui' import { Form, FormItem } from 'element-ui' import { Input } from 'element-ui' Vue.use(Button) Vue.use(Form) Vue.use(FormItem) Vue.use(Input) ``` 这样就完成了对Element UI组件的按引入。 4. 在main.js文件中引入自定义的element插件,使用命令`import '@/plugins/element.js'`。这样就可以在组件中正常使用Element UI组件了。例如,在组件的template中可以这样使用Button组件: ```html <template> <div id="app"> <el-button type="primary">按钮</el-button> </div> </template> ``` 以上就是按引入Vue Element UI组件的步骤。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Vue 项目中按引入 ElementUI](https://blog.csdn.net/qq_46376192/article/details/128792141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [计算机程序设计语言课程设计(VUE.js)及实践项目的例子.txt](https://download.csdn.net/download/weixin_44609920/88236928)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值