当前位置:   article > 正文

vue3项目如何按需引入ant design vue组件和图标_ant design二维码按需引入组件

ant design二维码按需引入组件

1.下载babel-plugin-import插件

npm install babel-plugin-import --save-dev

2.在babel.config文件中加入以下代码

  1. {
  2. "plugins": [
  3. ["import", { "libraryName": "ant-design-vue", "libraryDirectory": "es", "style": "css" }] // `style: true` 会加载 less 文件
  4. ]
  5. }

完整babel.config文件

  1. module.exports = {
  2. presets: [
  3. '@vue/cli-plugin-babel/preset'
  4. ],
  5. plugins: [
  6. ["import", { "libraryName": "ant-design-vue", "libraryDirectory": "es", "style": "css" }] // `style: true` 会加载 less 文件
  7. ]
  8. }

3.按需引入,新建antDesignVue.js文件

  1. // 引入样式
  2. import 'ant-design-vue/es/message/style/css'
  3. // 引入所需组件
  4. import {
  5. Button,
  6. Card,
  7. Spin,
  8. Col,
  9. Row,
  10. Tree,
  11. Select,
  12. Radio,
  13. Upload,
  14. Input,
  15. Modal,
  16. Dropdown,
  17. Menu,
  18. Layout,
  19. Form,
  20. Space,
  21. Switch,
  22. Popover,
  23. Checkbox,
  24. Breadcrumb,
  25. Avatar,
  26. Table,
  27. Divider,
  28. ConfigProvider,
  29. Popconfirm,
  30. Tabs,
  31. Drawer,
  32. Cascader,
  33. List
  34. } from 'ant-design-vue';
  35. // 引入所有图标
  36. import * as Icons from "@ant-design/icons-vue";
  37. // 按需引入图标
  38. //import { StepBackwardOutlined } from "@ant-design/icons-vue"; // 按需加载图标
  39. //const Icons = { StepBackwardOutlined }
  40. const components = [Modal, Button, Card, Spin, Col, Row, Tree, Select, Radio, Upload, Input, Dropdown, Menu, Layout, Form, Space, Switch, Popover, Checkbox, Breadcrumb, Avatar, Table, Divider, ConfigProvider, Popconfirm, Tabs, Drawer, Cascader, List];
  41. export default function setupAtnd(app) {
  42. // 注册图标
  43. Object.keys(Icons).forEach(key => {
  44. app.component(key, Icons[key])
  45. });
  46. // 全局挂载图标
  47. app.config.globalProperties.$icons = Icons;
  48. // 注册组件
  49. components.forEach((component) => {
  50. app.use(component)
  51. })
  52. }

4.在mainjs文件组件并注册

  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. // ant-design-vue
  4. import setupAtnd from "./antDesignVue.js"
  5. const app = createApp(App);
  6. // set up ant-desing-vue
  7. setupAtnd(app)
  8. app.mount('#app')

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

闽ICP备14008679号