import{_ as s,o as a,c as n,R as l}from"./chunks/framework.6241eaf2.js";const A=JSON.parse('{"title":"VantUI 组件按需加载","description":"","frontmatter":{},"headers":[],"relativePath":"guide/vue2/vant.md","filePath":"guide/vue2/vant.md","lastUpdated":1731315864000}'),p={name:"guide/vue2/vant.md"},o=l(`
项目采 用Vant 自动按需引入组件 (推荐)下 面安装插件介绍:
babel-plugin-import 是一款 babel
插件,它会在编译过程中将 import
的写法自动转换为按需引入的方式
npm i babel-plugin-import -D
在babel.config.js
设置
// 对于使用 babel7 的用户,可以在 babel.config.js 中配置
const plugins = [
[
"import",
{
libraryName: "vant",
libraryDirectory: "es",
style: true,
},
"vant",
],
];
module.exports = {
presets: [
["@vue/cli-plugin-babel/preset", { useBuiltIns: "usage", corejs: 3 }],
],
plugins,
};
项目在 src/plugins/vant.js
下统一管理组件,用哪个引入哪个,无需在页面里重复引用
// 按需全局引入 vant组件
import Vue from "vue";
import { Button, List, Cell, Tabbar, TabbarItem } from "vant";
Vue.use(Button);
Vue.use(Cell);
Vue.use(List);
Vue.use(Tabbar).use(TabbarItem);