当前位置:   article > 正文

三、手把手教你创建Vue3后台模板─────集成element-plus_elementplus官方文档

elementplus官方文档

想比vue2用的element-ui,element-plus是基于 Vue 3,面向设计师和开发者的组件库

element-plus官方文档:https://element-plus.org/zh-CN/#/zh-CN

配置@ = src 目录

因为Vite 客户端不支持node内置模块path的处理,我们使用path-browserify作为代替

npm install path-browserify --save-dev

  1. // vite.config.js
  2.   resolve: {
  3.     alias: {
  4.       path: 'path-browserify',
  5.       '@': resolve(__dirnameNew, 'src'),
  6.       '@views': resolve('./src/views')
  7.     }
  8.   }

项目中 集成scss

安装依赖

npm install sass --save-dev

scss集成起来比较方便,直接npm后就可以编写scss样式了

安装element-plus依赖

npm install element-plus --save

 接下来main注册依赖就可以了,这边采用的是全局注册

  1. // main.js
  2. import ElementPlus from 'element-plus'
  3. import 'element-plus/dist/index.css'

安装element-plus icon依赖

npm install @element-plus/icons-vue --save

npm install vite-plugin-svg-icons --save-dev

 src/components/SvgIcon/index.vue

  1. <template>
  2. <div v-if="isExternalIcon" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$attrs" />
  3. <svg :class="svgClass" aria-hidden="true" v-on="$attrs">
  4. <use :xlink:href="iconName" :fill="color" />
  5. </svg>
  6. </template>
  7. <script>
  8. import { isExternal } from '@/utils/validate'
  9. import { computed } from 'vue'
  10. export default {
  11. name: 'SvgIconIndex',
  12. props: {
  13. iconClass: {
  14. type: String,
  15. required: true
  16. },
  17. className: {
  18. type: String,
  19. default: ''
  20. },
  21. color: {
  22. // 设置图标颜色
  23. type: String,
  24. default: '#333'
  25. }
  26. },
  27. setup(props) {
  28. const isExternalIcon = isExternal(props.iconClass)
  29. const iconName = computed(() => `#icon-${props.iconClass}`)
  30. const svgClass = computed(() => {
  31. if (props.className) {
  32. return `svg-icon ${props.className}`
  33. }
  34. return 'svg-icon'
  35. })
  36. const styleExternalIcon = computed(() => ({
  37. mask: `url(${props.iconClass}) no-repeat 50% 50%`,
  38. '-webkit-mask': `url(${props.iconClass}) no-repeat 50% 50%`
  39. }))
  40. return { iconName, svgClass, isExternalIcon, styleExternalIcon }
  41. }
  42. }
  43. </script>
  44. <style scoped>
  45. .svg-icon {
  46. width: 1em;
  47. height: 1em;
  48. vertical-align: -0.15em;
  49. fill: currentColor;
  50. overflow: hidden;
  51. }
  52. .svg-external-icon {
  53. background-color: currentColor;
  54. mask-size: cover !important;
  55. display: inline-block;
  56. }
  57. </style>
  1. // import { isExternal } from '@/utils/validate'
  2. export function isExternal(path) {
  3. return /^(https?:|mailto:|tel:)/.test(path)
  4. }

 vue升级到3版本后,element plus引入icon是需要单独去引用的。

  1. // main.js
  2. import 'virtual:svg-icons-register' // 引入注册脚本
  3. import SvgIcon from '@/components/SvgIcon/index.vue' // svg component
  4. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  5. // 注册el-icon
  6. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  7. app.component(key, component)
  8. }

重置一下css 

npm install normalize.css --save

  1. // main.js
  2. import 'normalize.css/normalize.css' // CSS重置
  3. import '@/styles/index.scss' // global css

css样式自定义内容比较多,没用到的可以自行删除  

@/styles/index.scss

  1. @import './element-plus.scss';
  2. @import './module.scss';
  3. @import './reset.scss';
  4. body {
  5. height: 100%;
  6. -moz-osx-font-smoothing: grayscale;
  7. -webkit-font-smoothing: antialiased;
  8. text-rendering: optimizeLegibility;
  9. font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
  10. }
  11. label {
  12. font-weight: 700;
  13. }
  14. html {
  15. height: 100%;
  16. box-sizing: border-box;
  17. }
  18. #app {
  19. height: 100%;
  20. }
  21. *,
  22. *:before,
  23. *:after {
  24. box-sizing: inherit;
  25. }
  26. a:focus,
  27. a:active {
  28. outline: none;
  29. }
  30. a,
  31. a:focus,
  32. a:hover {
  33. cursor: pointer;
  34. color: inherit;
  35. text-decoration: none;
  36. }
  37. div:focus {
  38. outline: none;
  39. }
  40. .clearfix {
  41. &:after {
  42. visibility: hidden;
  43. display: block;
  44. font-size: 0;
  45. content: ' ';
  46. clear: both;
  47. height: 0;
  48. }
  49. }
  50. // main-container global css
  51. .app-container {
  52. padding: 20px;
  53. }

element-plus.scss

  1. // cover some element-plus styles
  2. .el-breadcrumb__inner,
  3. .el-breadcrumb__inner a {
  4. font-weight: 400 !important;
  5. }
  6. .el-upload {
  7. input[type='file'] {
  8. display: none !important;
  9. }
  10. }
  11. .el-upload__input {
  12. display: none;
  13. }
  14. // refine element ui upload
  15. .upload-container {
  16. .el-upload {
  17. width: 100%;
  18. .el-upload-dragger {
  19. width: 100%;
  20. height: 200px;
  21. }
  22. }
  23. }
  24. // dropdown
  25. .el-dropdown-menu {
  26. a {
  27. display: block;
  28. }
  29. }
  30. // to fix el-date-picker css style
  31. .el-range-separator {
  32. box-sizing: content-box;
  33. }

main.js代码

  1. import { createApp } from 'vue'
  2. import ElementPlus from 'element-plus'
  3. import 'element-plus/dist/index.css'
  4. import 'normalize.css/normalize.css' // CSS重置
  5. import '@/styles/index.scss' // global css
  6. import 'virtual:svg-icons-register' // 引入注册脚本
  7. import SvgIcon from '@/components/SvgIcon/index.vue' // svg component
  8. import * as ElementPlusIconsVue from '@element-plus/icons-vue'
  9. import App from './App.vue'
  10. const app = createApp(App)
  11. // 注册el-icon
  12. for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  13. app.component(key, component)
  14. }
  15. app.component('svg-icon', SvgIcon)
  16. app.use(ElementPlus)
  17. app.mount('#app')

完整vite.config.js配置 

  1. import { defineConfig } from 'vite'
  2. import vue from '@vitejs/plugin-vue'
  3. import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
  4. import { cwd } from 'process'
  5. import { resolve, dirname } from 'path'
  6. import { fileURLToPath } from 'url'
  7. const __filenameNew = fileURLToPath(import.meta.url)
  8. const __dirnameNew = dirname(__filenameNew)
  9. // https://vitejs.dev/config/
  10. export default defineConfig({
  11. base: '/',
  12. plugins: [
  13. vue(),
  14. createSvgIconsPlugin({
  15. // 指定需要缓存的图标文件夹
  16. iconDirs: [resolve(cwd(), 'src/icons/svg')],
  17. // 指定symbolId格式
  18. symbolId: 'icon-[dir]-[name]'
  19. })
  20. ],
  21. resolve: {
  22. alias: {
  23. path: 'path-browserify',
  24. '@': resolve(__dirnameNew, 'src'),
  25. '@views': resolve('./src/views')
  26. }
  27. },
  28. server: {
  29. host: '0.0.0.0',
  30. port: 8086, // 自定义端口
  31. open: true // 设置服务启动时是否自动打开浏览器
  32. }
  33. })

package.json

到这一步element-plus、scss、svg的引入就大功告成了。vue2跟vue3还是有区别的,配置起来还是要花一番功夫。能一步配置成功当然最好,大家遇到问题的话,欢迎评论区提出来。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/116827
推荐阅读
相关标签
  

闽ICP备14008679号