手把手教你寫一個uniapp通用頁面組件
前言
做移動端項目時為了兼容各種手機型號的界面,最好有一個統(tǒng)一的頁面組件對樣式做統(tǒng)一處理,例如:判斷是否顯示狀態(tài)欄,是否顯示頭部導(dǎo)航,是否空出底部區(qū)域等等,本篇會帶大家從零到一開發(fā)一個 uniapp 的通用頁面組件
需求
本次開發(fā)的頁面,組件,需要完成以下功能
- 可配置控制是否顯示原生導(dǎo)航,顯示狀態(tài)欄,并且也可以控制狀態(tài)欄顏色
- 可配置控制是否留出底部固定區(qū)域
開發(fā)
初始化頁面數(shù)據(jù)
- 編寫頁面組件類,獲取系統(tǒng)配置,初始化樣式數(shù)據(jù)
class Page {
constructor() {
this.system = uni.getSystemInfoSync()
?
this.init()
}
init = () => {
console.log(this.system);
}
}
?
export default Page- 頁面組件基本結(jié)構(gòu)
<template>
<view class="sf-page" :class="theme">
<!-- 頁面頭部 -->
<template v-if="customHeader">
<view class="sf-page-header">
<!-- 頭部核心 -->
<slot name="header"></slot>
</view>
</template>
<!-- 頁面主體 -->
<view class="sf-page-body">
<slot name="body"></slot>
</view>
<!-- 頁面底部 -->
<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>實現(xiàn)狀態(tài)欄與底部配置
- 通過系統(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- 頁面組件配置
<template>
<view class="sf-page" :class="theme">
<!-- 頁面頭部 -->
<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>
<!-- 頁面主體 -->
<view class="sf-page-body">
<slot name="body"></slot>
</view>
<!-- 頁面底部 -->
<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>頁面組件實例化Page 對象,這里注意解構(gòu)高度屬性時,需要使用toRefs 實現(xiàn)響應(yīng)式, 這樣,即可通過 customHeader,customFooter 控制相應(yīng)區(qū)域是否顯示,并且根據(jù)設(shè)置的 height 來控制對應(yīng)區(qū)域的高度, 也可通過 statusBarBG 控制狀態(tài)欄的顏色
<sf-page :customHeader="true" :customFooter="false" statusBarBG="#333333"> </sf-page>
頁面使用
<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è)計稿的需要,動態(tài)的控制是否顯示頭部導(dǎo)航或底部操作區(qū)域啦
總結(jié)
到此這篇寫一個uniapp通用頁面組件的文章就介紹到這了,更多相關(guān)uniapp通用頁面組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp使用高德地圖的超詳細步驟
- uniapp實現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能
- uniapp微信小程序打卡功能的詳細實現(xiàn)流程
- uniapp頁面間傳參的幾種方法實例總結(jié)
- uniapp開發(fā)打包多端應(yīng)用完整方法指南
- uniapp打包安卓App的兩種方式(云打包、本地打包)方法詳解
- 使用uniapp打包上架微信小程序完整教程
- uniApp微信小程序使用騰訊地圖定位功能及getLocation需要在app.json中聲明permission字段問題解決
- uniapp語音識別(訊飛語音)轉(zhuǎn)文字
- uniapp使用條件編譯#ifdef(跨平臺設(shè)備兼容)
相關(guān)文章
寫了10年的Javascript也未必全了解的連續(xù)賦值運算
很喜歡 蔡蔡 的這個標(biāo)題,實際蔡蔡已經(jīng)分析過了,這里借用了?;蛟S有點標(biāo)題黨的意思。看完就知了。2011-03-03
解讀IE和firefox下JScript和HREF的執(zhí)行順序
2008-01-01
使用TypeScript?V8來改進您的JavaScript代碼
TypeScript?V8是一個強大的JavaScript類型系統(tǒng),它可以幫助你發(fā)現(xiàn)JavaScript代碼中的錯誤和潛在問題,并在編譯時捕獲它們,以便您可以解決它們,TypeScript?V8為JavaScript提供一系列的類型注釋,包括自定義類型和其他高級功能2023-08-08
ECharts柱狀圖關(guān)閉鼠標(biāo)hover時的高亮樣式詳解
為了方便使用,echarts的餅圖中給加入了默認的hover高亮效果,下面這篇文章主要給大家介紹了關(guān)于ECharts柱狀圖關(guān)閉鼠標(biāo)hover時的高亮樣式的相關(guān)資料,需要的朋友可以參考下2023-04-04

