Vue2 引入使用ElementUI详解

1 安装

推荐使用 npm 的方式安装,它能更好地和 webpack打包工具配合使用。(本项目使用安装方式)

npm i element-ui -S

也可以使用其他的包管理起进行安装:

# Yarn
$ yarn add element-ui

# pnpm
$ pnpm install element-ui

2 引入

ElementUI分为全局引入和按需引入两种方式,一般在工程项目中,如果使用全局引入,则项目初始化时会导致不必要的资源加载,为提升项目性能,建议进行按需引入。以下我们对两种引入方式进行介绍。

2.1 全局引入
2.1.1 引入

在 main.js 中写入以下内容:

import Vue from 'vue';
import ElementUI from 'element-ui';
//样式文件需要单独引入
import 'element-ui/lib/theme-chalk/index.css';
import App from './App.vue';

Vue.use(ElementUI);

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

以上代码便完成了 Element 的引入。

2.1.2 使用

引入完成之后就可以使用组件了,如下示例为使用container组件和button组件:

效果如下:
在这里插入图片描述

代码如下:

<template>
  <el-container>
    <el-header>Header</el-header>
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-main>
          Main
          <el-button type="primary">按钮</el-button>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script>
export default {};
</script>
<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

2.2 按需引入

可以使用babel-plugin-component这个Babel插件。这样,你可以只引入你实际使用的组件和它们的样式,从而减小项目体积和构建时间。

2.2.1 引入

首先,安装 babel-plugin-component:

npm install babel-plugin-component -D

然后,将 .babelrc 或者babel.config.js文件修改为:

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

若想实现上图效果,按需引入时需要将使用的所有组件都引入进来,代码如下:

<template>
  <el-container>
    <el-header>Header</el-header>
    <el-container>
      <el-aside width="200px">Aside</el-aside>
      <el-container>
        <el-main>
          Main
          <el-button type="primary">按钮</el-button>
        </el-main>
        <el-footer>Footer</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>
<script>
import Vue from "vue";
import { Button, Container, Header, Aside, Main, Footer } from "element-ui";
Vue.use(Button);
Vue.use(Container);
Vue.use(Aside);
Vue.use(Main);
Vue.use(Footer);
Vue.use(Header);

export default {};
</script>
<style scoped>
.el-header,
.el-footer {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #d3dce6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>

按需引入组件,组件全部名称详见官网

3 总结

通常情况下,若是对性能没有要求时,可以使用全局导入方式引入所有组件,若对页面加载性能有要求,则最好使用按需加载方式引入组件,以防多余的资源加载增加页面初始化耗时。

### 如何在 Vue 2 项目中集成 Element UI 组件库 为了在 Vue 2 项目中成功引入并配置 Element UI 组件库,可以遵循以下方法: #### 安装依赖包 通过 npm 或者 yarn 来安装 `element-ui` 和其样式文件。对于基于 Node.js 的环境来说,命令如下所示: ```bash npm install element-ui --save ``` 或者如果使用的是 Yarn,则执行下面这条语句来完成同样的操作: ```bash yarn add element-ui ``` #### 配置 Webpack Loader 确保项目的 webpack 已经正确设置了处理 `.scss`, `.sass`, `.less` 文件的 loader,因为 Element UI 使用 Less 进行样式编写。 #### 导入整个 Element UI 库 可以在 main.js 中全局导入 Element UI 及其默认主题样式表,这样就可以在整个应用程序里访问所有的组件了。 ```javascript import Vue from 'vue'; import ElementUI from 'element-ui'; // 引入库 import 'element-ui/lib/theme-chalk/index.css'; // 引入样式 Vue.use(ElementUI); new Vue({ el: '#app', ... }); ``` #### 按需加载特定组件 (推荐方式) 考虑到性能优化方面的需求,建议只按需引入所需的单个组件而不是全部一次性载入。借助于像 babel-plugin-component 插件可以帮助实现这一点。 首先需要安装插件: ```bash npm install babel-plugin-component -D ``` 接着修改 .babelrc 文件以支持该功能: ```json { "plugins": [ ["component", [ { "libraryName": "element-ui", "styleLibraryName": "theme-chalk" } ]] ] } ``` 之后便可以根据实际需求单独引入各个组件而无需担心多余的资源被加载进来。 #### 创建页面组件实例化应用 创建一个新的 Vue 单文件组件用于展示如何利用已注册好的 Element UI 组件构建界面布局结构[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值