vue element ui 按需引入减少app.js体积

最近开发了一个项目属于一个项目集。包含很多功能,并且开发的人员也很多,就造成了打包过后,在生产环境第一次加载页面就很慢。网页出现白板时间很长。用户体验性不好。所以就针对第三方引入按需加载这里做了一个说明。

1:查看加载慢的原因。vue 有插件 “webpack-bundle-analyzer”,可视化打包图形模板。能够清晰的看见,那个板块体积很大

npm install webpack-bundle-analyzer

然后在vue.config.js 引入使用

const { defineConfig } = require("@vue/cli-service");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false, //false关闭命名规范
  configureWebpack: {
    module: {
 plugins: [new BundleAnalyzerPlugin()],
  },
});

最后运行启动代码。 就会默认加载一个可视化图形。

还有很多默认配置项,想要更清楚的了解 可以自行查阅

    // BundleAnalyzerPlugin 配置的具体配置项(默认)
    // new BundleAnalyzerPlugin({
    //   analyzerMode: 'server',
    //   analyzerHost: '127.0.0.1',
    //   analyzerPort: '8888',
    //   reportFilename: 'report.html',
    //   defaultSizes: 'parsed',
    //   openAnalyzer: true,
    //   generateStatsFile: false,
    //   statsFilename: 'stats.json',
    //   statsOptions: null,
    //   excludeAssets: null,
    //   logLevel: info
    // })

 我这里查看是因为element UI  全局引入了,但是很多组件我都没有使用,就造成体积很大。这里我把element UI  按需引入

element 按需引入 先要

npm install babel-plugin-component

引入element 按需引入插件。

然后在你的公共JS 文件中新建一个elementUI.js

在mian.js 中引入此文件。

按需引入文件内容

/**
 * 按实际使用 导出组件
 */
import Vue from "vue";
import {
  Button,
  Select,
  Input,
  Switch,
  Radio,
  Checkbox,
  Tag,
  TimePicker,
  DatePicker,
  Pagination,
  Avatar,
  Notification,
  Menu,
  Submenu,
  MenuItem,
  MenuItemGroup,
  Dropdown,
  Steps,
  Dialog,
  Tooltip,
  Popover,
  Card,
  Carousel,
  CarouselItem,
  Collapse,
  Timeline,
  Divider,
  Image,
  Form,
  FormItem,
  Tabs,
  Table,
  TableColumn,
  TabPane,
  Tree,
  Alert,
  Slider,
  Icon,
  Upload,
  Progress,
  Loading,
  Message,
  MessageBox,
} from "element-ui";
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Tree);
Vue.use(Alert);
Vue.use(Slider);
Vue.use(Icon);
Vue.use(Upload);
Vue.use(Progress);
Vue.use(Button);
Vue.use(Select);
Vue.use(Input);
Vue.use(Switch);
Vue.use(Radio);
Vue.use(Checkbox);
Vue.use(Tag);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(TimePicker);
Vue.use(DatePicker);
Vue.use(Pagination);
Vue.use(Avatar);
Vue.use(Notification);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Dropdown);
Vue.use(Steps);
Vue.use(Dialog);
Vue.use(Tooltip);
Vue.use(Popover);
Vue.use(Card);
Vue.use(Carousel);
Vue.use(CarouselItem);

Vue.use(Collapse);
Vue.use(Timeline);
Vue.use(Divider);
Vue.use(Image);
// Vue.use(MessageBox);

Vue.use(Loading.directive);
Vue.prototype.$loading = Loading.service;
Vue.prototype.$notify = MessageBox.confirm;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;

同理其他UI 组件也能这样使用。 按需引入是减少代码体积一个很重要的

要按引入Vue Element UI组件,你要进行以下几个步骤: 1. 首先,安装Element UI和babel-plugin-component。使用命令`npm i element-ui --save`来安装Element UI,然后使用命令`npm install babel-plugin-component -D`来安装babel-plugin-component插件。 2. 打开项目根目录下的babel.config.js文件,并进行配置。在presets数组中添加`["@babel/preset-env", { "modules": false }]`,在plugins数组中添加以下代码: ```javascript ["component", { libraryName: "element-ui", styleLibraryName: "theme-chalk" }] ``` 这个配置会将Element UI的组件按引入,并引入对应的样式。 3. 新建一个文件`/src/components/plugins/element.js`,并在这个文件引入Element UI组件。例如,如果你要使用Button、Form、FormItem和Input组件,你可以在element.js文件中写入以下代码: ```javascript import Vue from 'vue' import { Button } from 'element-ui' import { Form, FormItem } from 'element-ui' import { Input } from 'element-ui' Vue.use(Button) Vue.use(Form) Vue.use(FormItem) Vue.use(Input) ``` 这样就完成了对Element UI组件的按引入。 4. 在main.js文件引入自定义的element插件,使用命令`import '@/plugins/element.js'`。这样就可以在组件中正常使用Element UI组件了。例如,在组件的template中可以这样使用Button组件: ```html <template> <div id="app"> <el-button type="primary">按钮</el-button> </div> </template> ``` 以上就是按引入Vue Element UI组件的步骤。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [Vue 项目中按引入 ElementUI](https://blog.csdn.net/qq_46376192/article/details/128792141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [计算机程序设计语言课程设计(VUE.js)及实践项目的例子.txt](https://download.csdn.net/download/weixin_44609920/88236928)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值