vue引入Element-plus的全局引入与局部引入实例分析

今天小编给大家分享一下vue引入Element-plus的全局引入与局部引入实例分析的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

vue引入Element-plus的全局引入与局部引入实例分析  vue 第1张

首先下载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-componentsunplugin-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>

效果:

vue引入Element-plus的全局引入与局部引入实例分析  vue 第2张

扩展:

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

优点:集成比较简单

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

用法:npm install element-plus --save

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

vue引入Element-plus的全局引入与局部引入实例分析  vue 第3张

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

vue引入Element-plus的全局引入与局部引入实例分析  vue 第4张

vue引入Element-plus的全局引入与局部引入实例分析  vue 第5张

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

优点:包会小一些

缺点:引用比较麻烦一些

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

vue引入Element-plus的全局引入与局部引入实例分析  vue 第6张

vue引入Element-plus的全局引入与局部引入实例分析  vue 第7张

以上就是“vue引入Element-plus的全局引入与局部引入实例分析”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注蜗牛博客行业资讯频道。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:niceseo99@gmail.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

评论

有免费节点资源,我们会通知你!加入纸飞机订阅群

×
天气预报查看日历分享网页手机扫码留言评论电报频道链接