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博客

相关推荐
天蓝色的鱼鱼3 小时前
从“死了么”到“我在”:用uniCloud开发一款温暖人心的App
前端·uni-app
小徐_23333 小时前
uni-app 组件库 Wot UI 的 AI 友好型编程指南
前端·uni-app
CHB19 小时前
uni-app x 蒸汽模式 性能测试基准报告 Benchmark
uni-app·harmonyos
whinc20 小时前
🚀 两年小程序开发,我把踩过的坑做成了开源 Skills
前端·微信小程序·ai编程
Lupino1 天前
烧掉 10 刀 API 费,我才明白小程序虚拟列表根本不用“库”!
react.js·微信小程序
anyup1 天前
月销 8000+,uView Pro 让 uni-app 跨端开发提速 10 倍
前端·uni-app·开源
小溪彼岸2 天前
是时候给想做小程序的小伙伴泼盆冷水了
微信小程序
远山枫谷3 天前
一文理清页面/组件通信与 Store 全局状态管理
前端·微信小程序
木易士心4 天前
一文读懂:微信小程序云数据库直连原理与使用指南
微信小程序·serverless
willow4 天前
uniapp实战
uni-app