uniapp封装一个自定义的loading组件

成果:就大概是这样一个可以旋转的loading(黑边不用理,是我录屏的原因)

1.components/myLoading/myLoading.vue

注意一定是组件名和文件夹相同

<template>
	<view class="refresh-box" v-if="value">
		<!-- aspectFit这个属性是必须的,可以按比例缩放图片 -->
		<image src="../../static/images/square.png" mode="aspectFit" class="image"></image>
		<view class="text">Loading</view>
	</view>
</template>
 
<script>
export default {
	name: 'Loading',
	props: {
		value: {
			type: Boolean,
			default: false
		}
	}
};
</script>
 
<style lang="scss">
.refresh-box {
	display: flex;
	flex-direction: column;
	justify-content: center; /* 水平居中 */  
	align-items: center; /* 垂直居中 */  
	height: 400rpx;
	.image {
		margin-top: 100rpx;
		width: 60rpx;
		height: 60rpx;
		animation-iteration-count: infinite;
		animation-name: loading;
		animation-duration: 2000ms;
		animation-delay: 200ms;
		animation-timing-function: ease-in-out;
	}
	.text {
		font-size: 25rpx;
		text-align: center;
		margin-top: 20rpx;
		font-weight: 500;
		letter-spacing: 0px;
		line-height: 23.17px;
	}
}

@keyframes loading {
	0% {
		transform: rotate(0deg);
	}
	100% {
		transform: rotate(360deg);
	}
}
</style>

2.自己想要把组件放在哪里(我这里是src/xzhu/xzhu.vue)

html:
<myLoading  :value="isLoading"></myLoading>
//直接使用组件,名字就是自己命名的那个

data中:
//默认loading是不显示
isLoading:false

created(){
    this.isLoading=true;
	setTimeout(() => {  
		this.isLoading = false;  
	}, 4000); // 4000毫秒等于4秒
},

这里使用的是props来传递参数,将isLoading参数绑定到一个具有props属性的组件myLoading 上。在myLoading 组件中,通过props属性获取传递的参数。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Uniapp中可以通过自定义组件实现自定义导航栏插件的封装。下面是一个简单的示例: 1. 创建自定义组件uniapp项目中,可以在任意页面中创建一个自定义组件。在创建组件时,可以选择使用.vue文件或.js文件。 在组件中,可以通过props属性来接收外部传递的参数。同时,在组件中可以使用uni-app提供的API来实现导航栏的样式和交互效果。 下面是一个简单的自定义导航栏组件示例: ```html <template> <view class="nav-bar"> <view class="nav-bar__left" @click="clickLeft"> <slot name="left" /> </view> <view class="nav-bar__title">{{ title }}</view> <view class="nav-bar__right" @click="clickRight"> <slot name="right" /> </view> </view> </template> <script> export default { name: 'NavBar', props: { title: { type: String, default: '', }, }, methods: { clickLeft() { uni.navigateBack(); }, clickRight() {}, }, }; </script> <style scoped> .nav-bar { display: flex; justify-content: space-between; align-items: center; height: 44px; padding: 0 16px; background-color: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1); } .nav-bar__left, .nav-bar__right { width: 44px; height: 44px; display: flex; justify-content: center; align-items: center; } .nav-bar__title { font-size: 18px; font-weight: bold; } </style> ``` 2. 在页面中使用自定义组件 在需要使用自定义导航栏的页面中,可以通过引入自定义组件并传递参数来使用。 下面是一个示例: ```html <template> <view> <NavBar title="自定义导航栏" /> <view class="content">这是页面内容</view> </view> </template> <script> import NavBar from '@/components/NavBar'; export default { components: { NavBar, }, }; </script> <style> .content { padding: 16px; } </style> ``` 通过以上步骤,我们就可以封装一个简单的自定义导航栏插件。通过设置不同的参数,可以实现不同的导航栏样式和交互效果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值