vue3+ts+uniapp微信小程序顶部导航栏

这是colorui改的,不用就不用看啦

color-ui(https://docs.xzeu.com/#/)

  1. 新建component文件夹创建topNavigation.vue
c 复制代码
<template>

	<view>
		<view class="cu-custom" :style="'height:' + CustomBar + 'px'">
			<view class="cu-bar fixed" :style="styleBar" :class="[bgImage ? 'none-bg text-white bg-img' : '', bgColor]">
				<view class="action" @tap="BackPage" v-if="isBack">
					<text class="cuIcon-back"></text>
					<slot name="backText"></slot>
				</view>
				<view class="content" :style="[{ top: StatusBar + 'px' }]">
					<slot name="content"></slot>
				</view>
				<slot name="right"></slot>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>

import { onLoad } from '@dcloudio/uni-app'
import { defineComponent, ref } from 'vue';
const props = defineProps<{
	bgColor: {
		type: String,
		default: ''
	},
	isBack: {
		type: [Boolean, String],
		default: false
	},
	bgImage: {
		type: string,
		default: ''
	}
}>()
let CustomBar = ref(0)
const StatusBar = ref(0)
let styleProps=ref('background-image:url('+props.bgImage+');')
let styleBar=ref('')
onLoad(() => {
	CustomBar.value = uni.getStorageSync('customBar')
	StatusBar.value = uni.getStorageSync('statusBar')
	styleBar.value='height:'+CustomBar.value+'px;padding-top:'+StatusBar.value+'px;'
	if (styleProps) {
		styleBar.value = styleBar.value+styleProps.value;
	}
})
const BackPage = () => {
	// if (getCurrentPages().length < 2 && 'undefined' !== typeof __wxConfig) {
	// 	let url = '/' + __wxConfig.pages[0]
	// 	return uni.redirectTo({url})
	// }
	uni.navigateBack({
		delta: 1
	});
}
</script>

<style scoped></style>
  1. 定义为全局组件main.ts
c 复制代码
import { createSSRApp } from "vue";
import App from "./App.vue";
import cuCustom from './colorui/components/cu-custom.vue'
import pinia from './stores'
import TopNavigation from "./compontens/topNavigation.vue";

export function createApp() {
  const app = createSSRApp(App);
  app.component('cu-custom',cuCustom)
  app.component('top-nav',TopNavigation)
  app.use(pinia)
// app.config.globalProperties.$http
  return {
    app,
  };
}

3.App.vue获取customBar,statusBar

也可以用pinia存储

c 复制代码
<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app"

	onLaunch (()=>{
		uni.getSystemInfo({
			success: function (e) {
				let CustomBar=0
				let StatusBar=0
				if (e.platform == 'android') {
					CustomBar = e.statusBarHeight + 50;
				} else {
					CustomBar = e.statusBarHeight + 45;
				};
				// mp-weixin
				StatusBar = e.statusBarHeight;
				let custom = wx.getMenuButtonBoundingClientRect();
				CustomBar = custom.bottom + custom.top - e.statusBarHeight + 4;
			// 这个是MP-ALIPAY
				// StatusBar = e.statusBarHeight;
				// CustomBar = e.statusBarHeight + e.titleBarHeight;
			
				uni.setStorageSync('customBar',CustomBar)
				uni.setStorageSync('statusBar',StatusBar)

			}
		})
		})
	onShow(()=>{
		console.log('App Show')
	}),
	onHide(()=>{
		console.log('App Hide')
	})

</script>
<style>
/*每个页面公共css */
@import "colorui/main.css";
@import "colorui/icon.css";
</style>
  1. 使用
c 复制代码
<template>
  <view>
    <view>
      <top-nav bgColor="bg-greenac70" bgImage="https://iknow-pic.cdn.bcebos.com/b3fb43166d224f4a893e388d1bf790529922d18d"
       isBack="true">
        <block slot="backText">返回</block>
        <block slot="content">标题</block>
      </top-nav>
    </view>
  </view>

</template>
相关推荐
天***88964 小时前
小程序婚纱店摄影楼预约婚庆礼展示,小程序开发定制,会员下单档期系统
小程序
说私域4 小时前
开源AI智能名片链动2+1模式商城小程序下短视频电商变现与广告变现的对比研究
人工智能·小程序
homelook5 小时前
uniapp蓝牙demo
uni-app
天***88966 小时前
美业医疗美容院小程序,预约会员管理养生馆诊所肌护肤理疗系统,附源码交付
小程序
2501_915909061 天前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
toooooop81 天前
微信小程序轮播图高度自适应优化
微信小程序·小程序
郑州光合科技余经理1 天前
技术视角:海外版一站式同城生活服务平台源码解析
java·开发语言·uni-app·php·排序算法·objective-c·生活
StarChainTech1 天前
电动车租赁行业的核心需求:智能中控设备的选择与技术方案
物联网·微信小程序·小程序·软件需求·共享经济
计算机毕设指导61 天前
基于微信小程序的积分制零食自选平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
云起SAAS1 天前
老年美文文章图文短视频资讯阅读抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·老年美文文章图文短视频资讯阅读