uniapp公用返回组件

2024-06-18 16:36
文章标签 uniapp 组件 返回 公用

本文主要是介绍uniapp公用返回组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

uniapp写一个公用的头部组件,包含home和返回。

  1. 页面中的引用
    在这里插入图片描述
    2.在components文件夹下,新建一个navBar.vue
<template><view class="view-wrap"><view :style="{ height: barHeight }"></view><view class="nav-bar-wrap" :style="{ background: background }"><view class="status-bar" :style="{ height: statusHeight }"></view><view class="nav-bar" :style="{ padding: menuGap, gap: menuGap, height: menuHeight }"><view class="left" v-if="back":style="{ width: menuWidth, height: menuHeight, lineHeight: menuHeight }"><uni-icons type="arrow-left" size="20" :color="backColor" @click="handleBack"></uni-icons><uni-icons type="home-filled" size="20" :color="backColor" @click="handleHome"></uni-icons></view><view v-else:style="{ width: menuWidth, height: menuHeight, lineHeight: menuHeight }"></view><view class="logo-block" v-if="type == 'icon'"><image class="logo"src="https://img.js.design/assets/img/63eef16e94031f91576975f7.png#6f1b14d0e35a1527a5a6621e0b5125a8"></image></view><view :style="{ color: backColor, fontSize: titleFontSize }" v-if="type == 'title'">{{ title }}</view><view v-if="type == 'slot'" class="container" v-else><slot></slot></view><view class="right" :style="{ width: menuWidth, height: menuHeight }"></view></view></view></view>
</template><script>const {windowWidth,statusHeight,menuGap,menuWidth,menuHeight} = getApp().globalDataexport default {props: {background: {type: String,default: '#ffffff'},backOption: {default: false,type: Boolean},back: {type: Boolean},type: {type: String,default: 'title'},title: {type: String,default: ''},backColor: {type: String,default: '#000'},titleFontSize: {type: String,},},data() {return {barHeight: '32px',menuGap: '7px',menuWidth: '0px',menuHeight: '32px',statusHeight: '7px',borderRadius: '4px',keyWord: '',};},mounted() {this.statusHeight = statusHeight + 'px'this.menuGap = menuGap + 'px'this.menuWidth = menuWidth + 'px'this.menuHeight = menuHeight + 'px'this.borderRadius = menuHeight / 2 + 'px'this.barHeight = statusHeight + menuHeight + menuGap * 2 + 'px'uni.setStorageSync('barHeight', this.barHeight)},computed: {barWidth() {if (this.back) {return windowWidth - menuWidth - menuGap * 4 - 26 + 'px'} else {return windowWidth - menuWidth - menuGap * 3 + 'px'}}},methods: {//处理返回handleBack() {let that = thislet pages = getCurrentPages();let currPage = pages[pages.length - 1]; //当前页面let prevPage = pages[pages.length - 2];uni.navigateBack({delta: 1,success: function(e) {if (that.backOption) {let page = getCurrentPages().pop();if (page == undefined || page == null) return;page.onLoad();}}})uni.$emit('back')},handleHome() {uni.switchTab({url: '/pages/index/index'})},}}
</script><style lang="scss" scoped>.view-wrap {position: relative;}.nav-bar-wrap {position: fixed;top: 0;z-index: 99;width: 100%;left: 0;right: 0;// box-shadow: 0px 3px 6px 0px rgba(216, 216, 216, 0.16);.nav-bar {display: flex;justify-content: space-between;align-items: center;box-sizing: content-box;.left {// width: 26px;// width: 79px;border: 0.5px solid rgba(241, 241, 241, 1);border-radius: 30px;// height: 30px;// line-height: 30px;flex-shrink: 0;display: flex;justify-content: space-between;padding: 0 13px;box-sizing: border-box;// text-align: center;background-color: rgba(255, 255, 255, 0.8);}.logo-block {// margin-left: 45%;}.logo {width: 34.14px;height: 40.48px;}.container {}.right {flex-shrink: 0;}}}
</style>
  1. 在App.vue中设置全局变量
<script>export default {globalData: {},onLaunch: function() {this.initBounding();},onHide: function() {},methods: {initBounding() {const {windowWidth,windowHeight,statusBarHeight} =uni.getSystemInfoSync();let menuGap = 7;let menuWidth = 0;let menuHeight = 32;let statusHeight = 7;// #ifdef MPconst {top,left,right,width,height} =uni.getMenuButtonBoundingClientRect();menuGap = windowWidth - right;menuWidth = width;menuHeight = height;statusHeight = top - menuGap;// #endif// #ifdef APP-PLUSstatusHeight = statusBarHeight;// #endifthis.globalData.windowWidth = windowWidth;this.globalData.windowHeight = windowHeight;this.globalData.statusHeight = statusHeight;this.globalData.menuGap = menuGap;this.globalData.menuWidth = menuWidth;this.globalData.menuHeight = menuHeight;},},};
</script><style lang="scss">@import "@/common/index.scss";@import "@/common/common.scss";// 设置整个项目的背景色page {background: #F4F4F4;//font-family: "PingFang SC", "Helvetica Neue", "Helvetica", "Arial", sans-serif !important;font-family: "Source Han Sans CN", "Helvetica Neue", "Helvetica", "Arial", sans-serif;}
</style>
  1. 在main.js中注册全局组件
import App from './App'
import {formatImage,formatPrice,validatePhoneNumber,validateIDCard} from "@/utils/index.js"
import store from './store'// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = falseVue.prototype.formatImage = formatImage
Vue.prototype.formatPrice = formatPrice
Vue.prototype.validatePhoneNumber = validatePhoneNumber
Vue.prototype.validateIDCard = validateIDCard
Vue.prototype.$store = storelet onFun = uni.$on;
uni.$on = (eventName,obj) =>{
try {
uni.$off(eventName);
} catch (error) {}
onFun(eventName,obj);
}App.mpType = 'app'
const app = new Vue({store,...App
})
app.$mount()
// #endif// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {const app = createSSRApp(App)// 注册全局组件for (let key in components) {app.component(key,components[key])}return {app}
}
// #endif

效果图如下
在这里插入图片描述
可以返回上一页和home页,也可以配置自己喜欢的颜色。

这篇关于uniapp公用返回组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1072531

相关文章

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

springMVC返回Http响应的实现

《springMVC返回Http响应的实现》本文主要介绍了在SpringBoot中使用@Controller、@ResponseBody和@RestController注解进行HTTP响应返回的方法,... 目录一、返回页面二、@Controller和@ResponseBody与RestController