手把手教你寫一個(gè)uniapp通用頁(yè)面組件
前言
做移動(dòng)端項(xiàng)目時(shí)為了兼容各種手機(jī)型號(hào)的界面,最好有一個(gè)統(tǒng)一的頁(yè)面組件對(duì)樣式做統(tǒng)一處理,例如:判斷是否顯示狀態(tài)欄,是否顯示頭部導(dǎo)航,是否空出底部區(qū)域等等,本篇會(huì)帶大家從零到一開(kāi)發(fā)一個(gè) uniapp 的通用頁(yè)面組件
需求
本次開(kāi)發(fā)的頁(yè)面,組件,需要完成以下功能
- 可配置控制是否顯示原生導(dǎo)航,顯示狀態(tài)欄,并且也可以控制狀態(tài)欄顏色
- 可配置控制是否留出底部固定區(qū)域
開(kāi)發(fā)
初始化頁(yè)面數(shù)據(jù)
- 編寫頁(yè)面組件類,獲取系統(tǒng)配置,初始化樣式數(shù)據(jù)
class Page { constructor() { this.system = uni.getSystemInfoSync() ? this.init() } init = () => { console.log(this.system); } } ? export default Page
- 頁(yè)面組件基本結(jié)構(gòu)
<template> <view class="sf-page" :class="theme"> <!-- 頁(yè)面頭部 --> <template v-if="customHeader"> <view class="sf-page-header"> <!-- 頭部核心 --> <slot name="header"></slot> </view> </template> <!-- 頁(yè)面主體 --> <view class="sf-page-body"> <slot name="body"></slot> </view> <!-- 頁(yè)面底部 --> <template v-if="customFooter"> <view class="sf-page-footer"> <slot name="footer"></slot> </view> </template> </view> </template> ? <script setup> import { computed, toRefs } from "vue" import Page from './class/page.js' ? const props = defineProps({ customHeader: { type: Boolean, default: false }, customFooter: { type: Boolean, default: false }, }) ? const page = new Page() ? const theme = computed(() => { return uni.$theme.get() }) </script> ? <style> .sf-page { min-height: 100vh; width: 100%; } .sf-page-header { position: fixed; top: 0; left: 0; right: 0; background-color: #ffffff; z-index: 99; } .sf-page-body { ? } .sf-page-footer { position: fixed; bottom: 0; left: 0; right: 0; background-color: #ffffff; z-index: 99; } </style>
實(shí)現(xiàn)狀態(tài)欄與底部配置
- 通過(guò)系統(tǒng)
api
,獲取系統(tǒng)狀態(tài)欄高度
import { ref } from 'vue' ? class Page { constructor() { this.system = uni.getSystemInfoSync() this.statusBarHeight = 0 this.headerHeight = 45 this.footerHeight = 45 this.init() } init = () => { this.statusBarHeight = this.system.statusBarHeight } } ? export default Page
- 頁(yè)面組件配置
<template> <view class="sf-page" :class="theme"> <!-- 頁(yè)面頭部 --> <template v-if="customHeader"> <view class="sf-page-header"> <!-- 沉浸式狀態(tài)欄 --> <view :style="{ height: statusBarHeight + 'px', background: statusBarBG }"></view> <!-- 頭部核心 --> <view :style="{ height: headerHeight + 'px' }"> <slot name="header"></slot> </view> </view> <!-- 占位 --> <view :style="{ height: statusBarHeight + headerHeight + 'px' }"></view> </template> <!-- 頁(yè)面主體 --> <view class="sf-page-body"> <slot name="body"></slot> </view> <!-- 頁(yè)面底部 --> <template v-if="customFooter"> <view class="sf-page-footer"> <slot name="footer"></slot> </view> </template> </view> </template> ? <script setup> import { computed, toRefs } from "vue" import Page from './class/page.js' ? const props = defineProps({ customHeader: { type: Boolean, default: false }, customFooter: { type: Boolean, default: false }, statusBarBG: { type: String, default: 'none' } }) ? const page = new Page() const { headerHeight, statusBarHeight, footerHeight } = toRefs(page) ? const theme = computed(() => { return uni.$theme.get() }) </script>
頁(yè)面組件實(shí)例化Page
對(duì)象,這里注意解構(gòu)高度屬性時(shí),需要使用toRefs
實(shí)現(xiàn)響應(yīng)式, 這樣,即可通過(guò) customHeader,customFooter
控制相應(yīng)區(qū)域是否顯示,并且根據(jù)設(shè)置的 height
來(lái)控制對(duì)應(yīng)區(qū)域的高度, 也可通過(guò) statusBarBG
控制狀態(tài)欄的顏色
<sf-page :customHeader="true" :customFooter="false" statusBarBG="#333333"> </sf-page>
頁(yè)面使用
<sf-page :customHeader="true" :customFooter="true" statusBarBG="#333333"> <template v-slot:header> <view class=""> // ... 導(dǎo)航 </view> </template> <template v-slot:body> <view class="main"> // ... 內(nèi)容 </view> </template> <template v-slot:footer> <view class=""> // ... 底部操作 </view> </template> </sf-page>
這樣,就可以根據(jù)設(shè)計(jì)稿的需要,動(dòng)態(tài)的控制是否顯示頭部導(dǎo)航或底部操作區(qū)域啦
總結(jié)
到此這篇寫一個(gè)uniapp通用頁(yè)面組件的文章就介紹到這了,更多相關(guān)uniapp通用頁(yè)面組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp使用高德地圖的超詳細(xì)步驟
- uniapp實(shí)現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能
- uniapp微信小程序打卡功能的詳細(xì)實(shí)現(xiàn)流程
- uniapp頁(yè)面間傳參的幾種方法實(shí)例總結(jié)
- uniapp開(kāi)發(fā)打包多端應(yīng)用完整方法指南
- uniapp打包安卓App的兩種方式(云打包、本地打包)方法詳解
- 使用uniapp打包上架微信小程序完整教程
- uniApp微信小程序使用騰訊地圖定位功能及getLocation需要在app.json中聲明permission字段問(wèn)題解決
- uniapp語(yǔ)音識(shí)別(訊飛語(yǔ)音)轉(zhuǎn)文字
- uniapp使用條件編譯#ifdef(跨平臺(tái)設(shè)備兼容)
相關(guān)文章
動(dòng)態(tài)加載JavaScript文件的3種方式
第一種是使用document.write/writeln()方式,第二種使用jQuery,第三種是使用原生js方法,感興趣的小伙伴們可以參考一下2018-05-05寫了10年的Javascript也未必全了解的連續(xù)賦值運(yùn)算
很喜歡 蔡蔡 的這個(gè)標(biāo)題,實(shí)際蔡蔡已經(jīng)分析過(guò)了,這里借用了?;蛟S有點(diǎn)標(biāo)題黨的意思??赐昃椭?。2011-03-03JavaScript中常見(jiàn)的獲取當(dāng)前日期方法
在我們開(kāi)發(fā)的許多應(yīng)用程序都會(huì)用到某種日期功能,無(wú)論是內(nèi)容的創(chuàng)建日期還是活動(dòng)的時(shí)間戳等等,這篇文章主要給大家介紹了關(guān)于JavaScript中常見(jiàn)的獲取當(dāng)前日期方法,需要的朋友可以參考下2024-06-06解讀IE和firefox下JScript和HREF的執(zhí)行順序
2008-01-01JavaScript實(shí)現(xiàn)多文件拖動(dòng)上傳功能
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)多文件拖動(dòng)上傳功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04使用TypeScript?V8來(lái)改進(jìn)您的JavaScript代碼
TypeScript?V8是一個(gè)強(qiáng)大的JavaScript類型系統(tǒng),它可以幫助你發(fā)現(xiàn)JavaScript代碼中的錯(cuò)誤和潛在問(wèn)題,并在編譯時(shí)捕獲它們,以便您可以解決它們,TypeScript?V8為JavaScript提供一系列的類型注釋,包括自定義類型和其他高級(jí)功能2023-08-08ECharts柱狀圖關(guān)閉鼠標(biāo)hover時(shí)的高亮樣式詳解
為了方便使用,echarts的餅圖中給加入了默認(rèn)的hover高亮效果,下面這篇文章主要給大家介紹了關(guān)于ECharts柱狀圖關(guān)閉鼠標(biāo)hover時(shí)的高亮樣式的相關(guān)資料,需要的朋友可以參考下2023-04-04使用TypeScript實(shí)現(xiàn)楊輝三角的代碼示例
楊輝三角,又稱帕斯卡三角,是一個(gè)數(shù)學(xué)上非常有趣和重要的概念,它是一種數(shù)學(xué)結(jié)構(gòu),它不僅可以用于組合數(shù)學(xué),還可以應(yīng)用于代數(shù)、概率和許多其他領(lǐng)域,在本文中,我們將通過(guò)使用?TypeScript?來(lái)編寫楊輝三角的程序,同時(shí)深入探討?TypeScript?的類型系統(tǒng)2023-09-09