首页 > web前端 > Vue.js > 正文

vue引入Element-plus的全局引入与局部引入(附代码)

WBOY
发布: 2022-08-10 17:21:05
转载
7208人浏览过

本篇文章给大家带来了关于vue的相关知识,其中主要介绍了关于vue3集成element-plus使用全局引入以及局部引入方法的相关问题,下面一起来看一下,希望对大家有帮助。

vue引入Element-plus的全局引入与局部引入(附代码)

【相关推荐:javascript视频教程vue.js教程

首先下载element-plus

npm install element-plus
登录后复制

1、第一种方式,使用全局引入

引入element-plus的方式是全局引入,代表的含义是所有的组件和插件都会被自动注册,

立即学习前端免费学习笔记(深入)”;

优点:上手快

缺点:会增大包的体积

在main.ts文件中

import { createApp } from 'vue'
// 全局引入
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'
import store from './store'
 
const app = createApp(App)
app.use(router)
app.use(store)
app.use(ElementPlus)
app.mount('#app')
登录后复制

2、第二种方式,使用局部引入

局部引入也就是在开发中用到某个组件对某个组件进行引入,

<template>
  <div>
    <el-button>Default</el-button>
    <el-button type="primary">Primary</el-button>
    <el-button type="success">Success</el-button>
    <el-button type="info">Info</el-button>
    <el-button type="warning">Warning</el-button>
    <el-button type="danger">Danger</el-button>
    <el-button>中文</el-button>
  </div>
</template>
<script>
import { defineComponent } from 'vue'
// 局部引入
import { ElButton } from 'element-plus'
import 'element-plus/theme-chalk/el-button.css'
import 'element-plus/theme-chalk/base.css'
export default defineComponent({
  components: { ElButton },
  setup() {
    return {}
  }
})
</script>
 
<style></style>
登录后复制

但是这样我们在开发时每次使用都要手动在组件中引入对应的css样式,使用起来会比较麻烦

3、按需自动引入element-plus  推荐

需要安装unplugin-vue-components 和 unplugin-auto-import这两款插件

npm install -D unplugin-vue-components unplugin-auto-import
登录后复制

安装完成之后在vue.config.js文件中配置

// vue.config.js
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
module.exports = {
  outputDir: './build',
  // 和webpapck属性完全一致,最后会进行合并
  configureWebpack: {
    resolve: {
      alias: {
        components: '@/components'
      }
    },
    //配置webpack自动按需引入element-plus,
      plugins: [
        AutoImport({
          resolvers: [ElementPlusResolver()]
        }),
        Components({
          resolvers: [ElementPlusResolver()]
        })
      ]
  }
}
登录后复制

 按需自动引入配置完之后,在组件中可直接使用,不需要引用和注册 这里已经实现了按需自动移入Element-plus组件 组件中直接使用:

<template>
  <div>
    <el-button>Default</el-button>
    <el-button type="primary">Primary</el-button>
    <el-button type="success">Success</el-button>
    <el-button type="info">Info</el-button>
    <el-button type="warning">Warning</el-button>
    <el-button type="danger">Danger</el-button>
    <el-button>中文</el-button>
  </div>
</template>
<script>
import { defineComponent } from 'vue'
export default defineComponent({
  setup() {
    return {}
  }
})
</script>
 
<style></style>
登录后复制

效果: 

扩展:

方式一,全局引用(所有的组件全部集成)

优点:集成比较简单

缺点:组件与样式全部会打包,体积大

用法:npm install element-plus --save

在main.ts中,引用js与css文件

以About.vue页面为例,直接在页面中使用相关组件就行,组件已默认全局注册,不需要在页面中重新注册 

 

方式二:局部引用(按需引用)

 优点:包会小一些 

 缺点:引用比较麻烦一些

用法一:以About.vue页面为例,在页面中引用js文件,局部注册组件,样式依然是全局引用,官方推荐

【相关推荐:javascript视频教程vue.js教程

以上就是vue引入Element-plus的全局引入与局部引入(附代码)的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:脚本之家网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号