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

相关推荐
Z编程1 小时前
uniapp实现H5页面麦克风权限获取与录音功能
uni-app
低代码布道师1 小时前
加油站小程序实战教程10开通会员
前端·javascript·低代码·小程序
傻小胖2 小时前
UniApp 实现兼容 H5 和小程序的拖拽排序组件
小程序·uni-app
谢尔登3 小时前
【uni-app】页面跳转传参
服务器·网络·uni-app
咕噜签名7 小时前
运行小程序需要选择什么配置的服务器
运维·服务器·小程序
谢尔登7 小时前
【uni-app】axios 报错:Error: Adapter ‘http‘ is not available in the build
网络协议·http·uni-app
Monly218 小时前
Uniapp: 修改启动时的端口号
uni-app
.清和.9 小时前
【uniapp-兼容性处理】swiper在iOS上偶发出现后几张图片白屏情况
uni-app
玲子的猫10 小时前
微信小程序实现table样式,自带合并行合并列
微信小程序·小程序·notepad++
堕落年代20 小时前
Uniapp当中的async/await的作用
前端·javascript·uni-app