手把手教你寫一個uniapp通用頁面組件
前言
做移動端項目時為了兼容各種手機型號的界面,最好有一個統(tǒng)一的頁面組件對樣式做統(tǒng)一處理,例如:判斷是否顯示狀態(tài)欄,是否顯示頭部導航,是否空出底部區(qū)域等等,本篇會帶大家從零到一開發(fā)一個 uniapp 的通用頁面組件
需求
本次開發(fā)的頁面,組件,需要完成以下功能
- 可配置控制是否顯示原生導航,顯示狀態(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)響應式, 這樣,即可通過 customHeader,customFooter 控制相應區(qū)域是否顯示,并且根據(jù)設(shè)置的 height 來控制對應區(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="">
// ... 導航
</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)的控制是否顯示頭部導航或底部操作區(qū)域啦
總結(jié)
到此這篇寫一個uniapp通用頁面組件的文章就介紹到這了,更多相關(guān)uniapp通用頁面組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- uniapp使用高德地圖的超詳細步驟
- uniapp實現(xiàn)h5、app與微信小程序三端pdf文件下載和預覽功能
- uniapp微信小程序打卡功能的詳細實現(xiàn)流程
- uniapp頁面間傳參的幾種方法實例總結(jié)
- uniapp開發(fā)打包多端應用完整方法指南
- uniapp打包安卓App的兩種方式(云打包、本地打包)方法詳解
- 使用uniapp打包上架微信小程序完整教程
- uniApp微信小程序使用騰訊地圖定位功能及getLocation需要在app.json中聲明permission字段問題解決
- uniapp語音識別(訊飛語音)轉(zhuǎn)文字
- uniapp使用條件編譯#ifdef(跨平臺設(shè)備兼容)
相關(guān)文章
詳解Webpack loader 之 file-loader
這篇文章主要介紹了詳解Webpack loader 之 file-loader,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
TypeScript使用vscode監(jiān)視代碼編譯的過程
這篇文章主要介紹了TypeScript使用vscode監(jiān)視代碼編譯,使用tsc 文件名稱可以將ts文件轉(zhuǎn)化為js文件,js文件可以引入在html文件中直接使用,需要的朋友可以參考下2021-12-12
原生JavaScript實現(xiàn)頁面滾動監(jiān)聽的方法步驟
滾動監(jiān)聽事件一般網(wǎng)頁中的返回頂部按鈕都是通過滾動監(jiān)聽事件來實現(xiàn)的,本文給大家介紹了原生JavaScript實現(xiàn)頁面滾動監(jiān)聽的方法步驟,文中通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2025-03-03
基于KO+BootStrap+MVC實現(xiàn)的分頁控件代碼分享
本段js和html兩段代碼實現(xiàn)分頁控件效果,下面通過本文給大家詳細介紹下基于KO+BootStrap+MVC實現(xiàn)的分頁控件,非常不錯,感興趣的朋友一起看看吧2016-11-11

