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

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

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文件夹!

!!!!必须是wxcomponents文件夹!

!!!!必须是wxcomponents文件夹!

(原因:和微信开发者工具内部编译文件名一致,具体原因不详)

3、将node_modules/@vant/weapp/下的dist文件夹复制粘贴到wxcomponents文件夹下(wxcomponents/dist)或者(wxcomponents/vant/dist)等等确保文件正确

        截图如下:

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

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

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

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

 

 6、使用引入的模块:

运行结果:

代码:

<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 微信小程序 Loading 模板 在开发微信小程序时,提供良好的用户体验至关重要。Loading 动画可以在页面加载过程中给用户提供反馈,告知其当前状态并提升整体交互体验。 对于 UniApp 开发者来说,实现一个简单的 loading 效果可以通过多种方式完成。下面是一个基于 Vue 组件特性的简单例子: #### 使用 v-if 和 CSS 实现基础的 Loading 提示框 ```html <template> <view class="container"> <!-- 显示加载动画 --> <view v-if="isLoading" class="loading-mask"> <image src="/static/loading.gif"></image> <!-- 加载中的 GIF 图片 --> <text>正在加载...</text> </view> <!-- 正常显示内容区域 --> <view v-else> {{ content }} </view> </view> </template> <script> export default { data() { return { isLoading: true, // 控制是否显示加载提示 content: '这里是主要内容' } }, onLoad(){ setTimeout(() => { this.isLoading = false }, 2000); //模拟异步操作后的隐藏加载层 } } </script> <style scoped> .loading-mask{ position: fixed; top: 0; bottom: 0; left: 0; right: 0; background-color: rgba(255, 255, 255, .8); display: flex; justify-content: center; align-items: center; flex-direction: column; } .loading-mask image{ width: 60px;height: 60px;} .loading-mask text{ font-size: 14px;color:#999;margin-top:10px;text-align:center;} </style> ``` 此代码片段展示了如何通过 `v-if` 来控制视图中不同部分的渲染逻辑,并利用内联样式来定义加载指示器的位置和外观[^1]。 为了进一步增强视觉效果,还可以考虑引入第三方库如 uView 或 Vant Weapp 中预设好的组件来进行更复杂的定制化设计。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值