uniapp微信小程序引入vant组件库

1、首先要有uniapp项目,根据vant官方文档使用yarn或npm安装依赖:

bash 复制代码
1、 yarn init 或 npm init
 
2、 # 通过 npm 安装
    npm i @vant/weapp -S --production
 
    # 通过 yarn 安装
    yarn add @vant/weapp --production
 
    # 安装 0.x 版本
    npm i vant-weapp -S --production

安装完依赖如下:

2、在uniapp项目根目录下(也可以指定目录)本案例在根目录下新建:wxcomponents文件夹

必须是wxcomponents文件夹!

(原因:和微信开发者工具内部编译文件名一致)
3、将node_modules/@vant/weapp/下的dist文件夹复制粘贴到wxcomponents文件夹下(wxcomponents/dist)或者(wxcomponents/vant/dist)等等确保文件正确

4、配置样式在App.vue文件中:

javascript 复制代码
<style lang="scss">
	/*每个页面公共css */
/* #ifdef MP-WEIXIN */
	@import "/wxcomponents/dist/common/index.wxss"; //路径要确保正确!!!!
/* #endif */	
</style>

5、配置按需引入vant组件模块在pages.json文件夹中:

javascript 复制代码
"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "uni-app",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8",
		"usingComponents": {
			"van-button": "/wxcomponents/dist/button/index",
			"van-circle": "/wxcomponents/dist/circle/index"
		}
	},

6、使用引入的模块:

运行结果:

代码:

javascript 复制代码
<template>
	<view class="content">
		<van-button type="default">默认按钮</van-button>
		<van-button type="primary">主要按钮</van-button>
		<van-button type="info">信息按钮</van-button>
		<van-button type="warning">警告按钮</van-button>
		<van-button type="danger">危险按钮</van-button>
		<view style="margin-top: 30rpx;">
			<van-circle :value="value" :color="gradientColor" text="渐变色" />
		</view>
	</view>
</template>
 
<script>
	export default {
		data() {
			return {
				value: 0, //进度
				gradientColor: { //渐变色
					'0%': '#ffd01e',
					'50%': '#aa55ff',
					'100%': '#ee0a24',
				},
				item: null,
			}
		},
		onUnload() {
			clearInterval(this.item)
			this.item = null
		},
		onLoad() {
			this.item = setInterval(() => {
				if (this.value >= 100) {
					this.value = 0
					return
				} else {
					this.value += 10
				}
			}, 1000)
		},
		methods: {
 
		}
	}
</script>
 
<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
</style>

本文为转载文章,仅供本人记录日常问题

原文链接:uniapp微信小程序引入vant组件库_uniapp怎么配置vant的按需导入-CSDN博客

相关推荐
ღ᭄ꦿ࿐Never say never꧂1 小时前
微信小程序 Button 组件 open-type 完全指南:从用户信息获取到客服分享的实战应用
spring boot·微信小程序·小程序·uni-app·vue3
你真的可爱呀2 小时前
uniapp学习【路由跳转 +数据请求+本地存储+常用组件】
前端·学习·uni-app
2501_915106328 小时前
iOS 26 APP 性能测试实战攻略:多工具组合辅助方案
android·macos·ios·小程序·uni-app·cocoa·iphone
巨神峰SEO11 小时前
支付宝小程序 SEO 实战:鲜花送达类小程序抢占搜索流量指南
小程序·支付宝小程序排名优化·支付宝小程序seo·支付宝小程序排名·支付宝优化
凉辰12 小时前
微信小程序uni.request 返回值存在精度丢失问题
微信小程序·小程序
李慕婉学姐13 小时前
【开题答辩过程】以《基于微信小程序教学评价平台的设计与实现》为例,不会开题答辩的可以进来看看
微信小程序·小程序
计算机学姐13 小时前
基于微信小程序的垃圾分类管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
weixin_lynhgworld16 小时前
剧本杀小程序系统开发:内容生态与商业模式的双轮驱动
大数据·小程序
博客zhu虎康16 小时前
微信小程序报错 ubepected character `的style换行问题
微信小程序
计算机徐师兄16 小时前
Java基于SpringBoot的农场管理系统小程序【附源码、文档说明】
java·微信小程序·小程序·农场管理系统小程序·java农场管理系统小程序·java农场管理系统微信小程序·农场管理微信小程序