当前位置:网站首页>微信小程序标题栏封装
微信小程序标题栏封装
2022-08-05 10:50:00 【程序猿向前跑】
标题栏一般都是居中的,但有时需要对标题栏进行样式等修改,就需要定制.
首先是组件的微信wxml相当于html
wxml
<!--base-ui/nav-bar-2/index.wxml-->
<view class="nav-container" style="height:{
{navigationBarAndStatusBarHeight}}px;">
<view style="height: {
{statusBarHeight}}px;"></view>
<view class="nav-bar" style="height: {
{navigationBarHeight}}px;">
<view class="nav-left-arrow" bindtap="handleLeftBtnClick">
<van-icon name="arrow-left" wx:if="{
{isShow}}"/>
</view>
<view class="nav-title">{
{
navTitle}}</view>
</view>
</view>
index.json
引用对应的组件
{
"component": true,
"usingComponents": {
"van-icon": "@vant/weapp/icon/index"
}
}
index.js
组件相关的js
import {
useGetNavBarInfo
} from "../../hooks/index";
Component({
properties:{
navTitle:{
type:String
},
isShow:{
type:Boolean,
value:true
}
},
data:{
//状态栏高度
statusBarHeight: 0,
//导航栏高度
navigationBarHeight: 0,
//胶囊按钮的高度
menuButtonHeight: 0,
//状态栏加导航栏高度
navigationBarAndStatusBarHeight: 0,
},
lifetimes:{
ready:async function(){
await this.getrelateBarHeights();
}
},
methods:{
async getrelateBarHeights(){
let {
statusBarHeight,
navigationBarHeight,
menuButtonHeight,
navigationBarAndStatusBarHeight}=
await useGetNavBarInfo();
this.setData({
statusBarHeight,
navigationBarHeight,
menuButtonHeight,
navigationBarAndStatusBarHeight
})
},
handleLeftBtnClick(){
let pages=getCurrentPages();
console.log(pages)
if(pages.length>1){
wx.navigateBack({
delta: 0,
})
}else{
wx.reLaunch({
url: '/pages/home/index',
})
}
}
}
})
/* base-ui/nav-bar-2/index.wxss */
.nav-container{
width: 100%;
top: 0;
background-color:rgb(25,158,216);
color: #ffffff;
}
.nav-container .nav-bar{
display: flex;
justify-content: flex-start;
align-items: center;
}
.nav-container .nav-bar .nav-title{
margin-left: 20rpx;
}
js文件
import {
useSetNavbarInfo} from "./useSetNavbarInfo.js";
import {
useShareInfo} from "./useShareInfo.js";
import {
useHandleIsRegAction} from "./useHandleIsRegAction.js";
import {
useVantDialog} from "./useVantDialog.js";
import {
useHandleStatusCode} from "./useHandleStatusCode.js";
import {
useGetNavBarInfo} from "./useGetNavBarInfo.js";
import {
useHandleRoute,redirectNavigateTo} from "./useHandleRoute.js";
import {
useGetAuthorization} from "./useGetAuthorization.js";
import {
useGetStoreParams} from "./useGetStoreParams.js";
import {
useShowLoading} from "./useShowLoading.js";
import {
useSelectItemAndRefresh} from "./useSelectItemAndRefresh.js";
export {
useGetNavBarInfo,
useShareInfo,
useHandleIsRegAction,
useVantDialog,
useHandleStatusCode,
useSetNavbarInfo,
useHandleRoute,
redirectNavigateTo,
useGetAuthorization,
useGetStoreParams,
useShowLoading,
useSelectItemAndRefresh
}
import {
deviceStore
} from "../store/index";
export function useGetNavBarInfo(){
return new Promise((resolve)=>{
deviceStore.onStates(["statusBarHeight", "navigationBarHeight", "menuButtonHeight","navigationBarAndStatusBarHeight"], ({
statusBarHeight, navigationBarHeight, menuButtonHeight, navigationBarAndStatusBarHeight }) => {
resolve({
statusBarHeight,
navigationBarHeight,
menuButtonHeight,
navigationBarAndStatusBarHeight
});
})
})
}
存储公共状态下的变量
import {
HYEventStore
} from "hy-event-store";
const deviceStore=new HYEventStore({
state:{
//状态栏高度
statusBarHeight:0,
//菜单按钮的高度
menuButtonHeight:0,
//导航栏的高度
navigationBarHeight:0,
//导航栏+状态栏高度
navigationBarAndStatusBarHeight:0,
},
action:{
}
})
export {
deviceStore
}
大功完成,打工人是真的累
边栏推荐
- Header file search rules when compiling with GCC
- Getting started with Polkadot parachain development, this article is enough
- 012_SSS_ Improving Diffusion Model Efficiency Through Patching
- SQL外连接之交集、并集、差集查询
- Voice-based social software development - making the most of its value
- PostgreSQL 2022 报告:流行度上涨,开源、可靠性和扩展是关键
- How does the official account operate and maintain?Public account operation and maintenance professional team
- 拓朴排序例题
- 解决【命令行/终端】颜色输出问题
- MySQL 中 auto_increment 自动插入主键值
猜你喜欢
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
MySQL 中 auto_increment 自动插入主键值
深入理解 Istio 流量管理的超时时间设置
字节一面:TCP 和 UDP 可以使用同一个端口吗?
sqlserver编写通用脚本实现获取一年前日期的方法
FPGA: Use of the development environment Vivado
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
FPGA:开发环境Vivado的使用
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
Leetcode刷题——623. 在二叉树中增加一行
随机推荐
阿里全新推出:微服务突击手册,把所有操作都写出来了PDF
STM32入门开发:编写XPT2046电阻触摸屏驱动(模拟SPI)
OpenHarmony如何查询设备类型
【 temperature warning program DE development 】 event driven model instance
字节一面:TCP 和 UDP 可以使用同一个端口吗?
导火索:OAuth 2.0四种授权登录方式必读
导火索:OAuth 2.0四种授权登录方式必读
Header file search rules when compiling with GCC
How OpenHarmony Query Device Type
【综合类型第 35 篇】程序员的七夕浪漫时刻
第五章:redis持久化,包括rdb和aof两种方式[通俗易懂]
Leetcode刷题——623. 在二叉树中增加一行
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
three objects are arranged in a spherical shape around the circumference
数据可视化(一)
数据可视化(二)
[Android] How to use RecycleView in Kotlin project
Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
第四章:activiti流程中,变量的传递和获取流程变量 ,设置和获取多个流程变量,设置和获取局部流程变量「建议收藏」