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>
相关推荐
Swift社区4 小时前
H5 与 ArkTS 通信的完整设计模型
uni-app·harmonyos
说私域6 小时前
短视频私域流量池的变现路径创新:基于AI智能名片链动2+1模式S2B2C商城小程序的实践研究
大数据·人工智能·小程序
小溪彼岸7 小时前
uni-app小白从0开发一款鸿蒙Next应用到上线
uni-app·harmonyos
毕设源码-邱学长8 小时前
【开题答辩全过程】以 基于微信小程序的松辽律所咨询系统的设计与实现为例,包含答辩的问题和答案
微信小程序·小程序
一颗小青松9 小时前
uniapp app端使用uniCloud的unipush
uni-app
+VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue物流配送中心信息化管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·小程序·课程设计
说私域10 小时前
B站内容生态下的私域流量运营创新:基于AI智能名片链动2+1模式与S2B2C商城小程序的融合实践
人工智能·小程序·流量运营
计算机毕设指导611 小时前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
qq_124987075311 小时前
基于微信小程序的宠物交易平台的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
cngm11012 小时前
uniapp+springboot后端跨域以及webview中cookie调试
spring boot·后端·uni-app