uniapp封装自定义导航栏组件

问题:用uniapp写小程序的时候,原生导航栏满足不了需求,uniapp官网推荐的占位view方法在不同的手机上显示有问题

解决方法:自定义导航栏

步骤:

  • 封装导航栏组件(用于复用)
  • 通过uni.getSystemInfoSync()uni.getMenuButtonBoundingClientRect()接口获取导航栏位置信息,动态设置位置
  • 将获取到的参数传递给组件

01.封装导航栏组件

在项目根目录下新建components文件夹,在components里新建目录navbar,新建navbar.vue
在这里插入图片描述

<template>
	<view class="status_bar">
		<view class="title">{{title}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//动态参数
				title:"",
				buttonHeight:0,
				statusBarHeight:0,
				buttonTop:0
			}
		},
		mounted() {

		}
	}
</script>

<style lang="scss" scoped>
/*默认样式设置*/
.status_bar{
	width: 100%;
	position: relative;
	.title{
		width: 100%;
		text-align: center;
		position: absolute;
		left: 0;
	}
}
</style>

02.获取导航栏位置参数

uniapp官网给了uni.getSystemInfoSync()uni.getMenuButtonBoundingClientRect()两个接口用于获取手机顶部状态栏高度和微信中的胶囊按钮位置

  • uni.getSystemInfoSync()获取手机顶部状态栏参数
  • uni.getMenuButtonBoundingClientRect()获取微信中的胶囊按钮参数
    在这里插入图片描述
    将所需参数封装成对象放入vuex中
    在这里插入图片描述
    vuex
    在这里插入图片描述

03.组件获取vuex中的参数

navbar.vue

<template>
	<view class="status_bar" :style="{height:statusBarHeight+'px'}">
		<view class="title" :style="{height:buttonHeight+'px',top:buttonTop+'px',lineHeight:buttonHeight+'px'}">{{title}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//动态参数
				title:"",
				buttonHeight:0,
				statusBarHeight:0,
				buttonTop:0
			}
		},
		mounted() {
			//从vuex里获取参数
			this.statusBarHeight=this.$store.state.statusBarHeight
			this.buttonHeight=this.$store.state.buttonHeight
			this.title=this.$store.state.title;
			this.buttonTop=this.$store.state.buttonTop;
		}
	}
</script>

<style lang="scss" scoped>
/*默认样式设置*/
.status_bar{
	width: 100%;
	position: relative;
	.title{
		width: 100%;
		text-align: center;
		position: absolute;
		left: 0;
	}
}
</style>

04.预览

微信开发者工具中
在这里插入图片描述
手机上
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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> ``` 通过以上步骤,我们就可以封装一个简单的自定义导航栏插件。通过设置不同的参数,可以实现不同的导航栏样式和交互效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值