亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

手把手教你寫一個(gè)uniapp通用頁(yè)面組件

 更新時(shí)間:2022年12月15日 16:05:11   作者:我的代碼果然有問(wèn)題  
uniapp中每個(gè)頁(yè)面可以理解為一個(gè)單頁(yè)面組件,下面這篇文章主要給大家介紹了關(guān)于如何寫一個(gè)uniapp通用頁(yè)面組件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

做移動(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論