uniapp的通用頁面及組件基本封裝

1.基本布局頁面

適用于自定義Navbar頭部

<template><view :style="{ background : param.bgColor , height: '100%' }"><block v-if="param.noHead"><slot name="head"></slot></block><block v-if="!param.noHead"><view class="bar" :style="{height : systemInfo.statusBarHeight + 'px', background: param.barBgColor }"></view><view class="headBox" :style="{ height: param.headHight + 'px',background: param.headBgColor,}"><view class="leftBox"><slot name="left"><view class="back" v-if="autoBack" @click="uni.navigateBack({ delta: 1 })"><XIcon name="back" blod></XIcon></view></slot></view><view class="centerBox"><slot name="center"><view class="title">{{param.title}}</view></slot></view><view class="rightBox" :style="{ paddingRight: !slots.right ? 0 + 'px' : (menuButton.width + 12) + 'px' }"><slot name="right"></slot></view></view></block><slot name="nav"></slot><scroll-view @scrolltolower="emits('tolower',true)" scroll-y id="calcBasePoint" :style="`height:${ rollHight }px;overflow-y: auto;`"><slot name="container"></slot></scroll-view></view>
</template><!-- 適用于自定義Navbar頭部 -->
<script setup lang="ts">import { onMounted , ref , useSlots } from "vue";import { getPageResidueDIST } from "@/utils/common";import XIcon from "@/element/XIcon.vue"const param = withDefaults(defineProps<{barBgColor: string;headHight: number;headBgColor: string;autoBack: boolean;title: string;noHead: boolean;bgColor: string;}>(),{bgColor: '#FAFAFA',headHight: 44,headBgColor: '#ffffff',barBgColor: '#ffffff',autoBack: true,title: '',noHead: false})const emits = defineEmits<{( e: 'tolower' , status : boolean ) : void,(e : 'sysInfo', data : any ) : void}>();const slots = useSlots();const systemInfo = ref<any>({});const menuButton = ref<any>({ width: 0 });const init = async () => {systemInfo.value = await uni.getSystemInfo();// #ifndef APP || H5menuButton.value = uni.getMenuButtonBoundingClientRect();// #endifemits('sysInfo',{system: systemInfo.value,menuBtn: menuButton.value});};onMounted(() => {init();setRollHight();});let rollHight = ref<number>(0);async function setRollHight() {let calcPoint = await getPageResidueDIST('#calcBasePoint');console.log(systemInfo.value?.safeAreaInsets?.bottom);rollHight.value = calcPoint - (!systemInfo.value?.safeAreaInsets?.bottom ?  0 : systemInfo.value?.safeAreaInsets?.bottom) ;// #ifdef APP || MP-WEIXINrollHight.value = rollHight.value - systemInfo.value.statusBarHeight - param.headHight;// #endif}</script><style scoped lang="scss">.headBox {display: flex;align-items: center;justify-content: space-between;padding: 0 12px;width: 100%;box-sizing: border-box;.leftBox {flex: 1;display: flex;justify-content: flex-start;}.centerBox {flex: 2;display: flex;justify-content: center;text-align: center;.title {font-size: 16px;color: #303133;}}.rightBox {flex: 1;display: flex;justify-content: flex-end;}}</style>

2.阿里iocn

注意使用阿里icon需要下載阿里的字體圖標,然后把css的文件引入到app.vue,就可以使用iconfont icon-xxx,是你圖標名稱

<template><view><text :class='`iconfont icon-${param.name}`' :style="{ fontSize: param.size + 'rpx' , color: param.color , fontWeight : param.blod ? '400' : 'bloder' }" ></text></view>
</template><script lang="ts" setup>import { onBeforeMount, ref } from "vue";interface Prop {name: string;size?: number;color? : string;blod?: false;}const param = withDefaults(defineProps<Prop>(),{size: 38});</script><style lang="scss" scoped>
</style>

3.緩存工具

這里擴展了一個記錄歷史查看

export function setCache(key : string, value : any ) {uni.setStorageSync(key,value);
}export function getCache(key : string) : any | null {return uni.getStorageSync(key);
}export function removeCache(key : string)  {return uni.removeStorageSync(key);
}interface Record {time: number;key: any;
}export function saveHistory(key : string,val : any) {let localHistory : Array<Record> = getCache(key);if(!localHistory) { localHistory = [] }if(localHistory.some(item => item.key == val)) { return }localHistory.unshift({time: new Date().getTime(),key: val});setCache(key,localHistory);
}export function removeHistory(key : string,val : any) : number {let row : number = 0;let localHistory : Array<Record> = getCache(key);if(!localHistory) { localHistory = [] }row = localHistory.length;localHistory = localHistory.filter(item => {if (item.key === val) {row++;return false;}return true;});setCache(key,localHistory);return row - localHistory.length;
}

4.公共工具類

獲取位置、計算元素剩余搞定 , 頁面跳轉

import { getCurrentInstance } from "vue";/*** 根據類型跳轉頁面并傳遞數據* * @param type - 跳轉類型:*               - 0: 使用 `uni.navigateTo` 跳轉,保留當前頁面,跳轉到新頁面*               - 1: 使用 `uni.switchTab` 跳轉,切換到指定 tabBar 頁面* @param url - 目標頁面的路徑* @param key ? - 事件通道中傳遞數據的鍵名* @param data ? - 通過事件通道傳遞的數據*/
export function skipPage(type : number,url : string,key ? : string , data ? : any) {switch (type){case 0:uni.navigateTo({url,success(res) {res.eventChannel.emit(key,data);}	})break;case 1:uni.switchTab({url,success(res) {res.eventChannel.emit(key,data);}	})break;default:console.log('接入中!');break;}
}export async function getPageResidueDIST(nodeKey: string) : Promise<number> {const query = uni.createSelectorQuery().in(getCurrentInstance());let systemInfo = await uni.getSystemInfo();let nodeToTop : number = await new Promise((ok, _) => {query.select(nodeKey).boundingClientRect((data: any) => {ok(data.top);}).exec();});return systemInfo.windowHeight - nodeToTop;
}export async function getSystemInfo() {return await uni.getSystemInfo();
}export async function getMenuButton() {// #ifndef APP || H5return uni.getMenuButtonBoundingClientRect();// #endifreturn { width: 0  }
}export async function getElementHight(nodeKey: string) : Promise<number> {const query = uni.createSelectorQuery().in(getCurrentInstance());let nodeToTop : number = await new Promise((ok, _) => {query.select(nodeKey).boundingClientRect((data: any) => {ok(data.height);}).exec();});return nodeToTop;
}interface Point {latitude: number;longitude: number;
}export async function initLocation() : Promise<any> {return await uni.getLocation({type: 'wgs84',geocode:true});
}export async function getAddress() : Promise<any> {return new Promise(( ok ) =>{uni.chooseLocation({success: function (res) {ok({code: 200,address: res.address,latitude: res.latitude,longitude: res.longitude,name: res.name})},fail() {ok({code: 201})}	});})
}

5.網絡請求類

import { env } from "@/env"export default function() {interface AskModel {url: string,data?: anymethod?: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'}interface ApiRes<T> {code: number;msg: string;data: T;}async function orderMealAsk<T>( option : AskModel ) : Promise<ApiRes<T>> {return new Promise(( resolve, reject ) => {uni.request({url:  env.orderMealReq + option.url, data: option.data || {},method: option.method || 'GET',header: {'content-type':'application/json' },success: (res) => {resolve(res.data as ApiRes<T>);},fail(res) {reject(res);}});})}return {  orderMealAsk }
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/79616.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/79616.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/79616.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于MTF的1D-2D-CNN-GRU-Attention時序圖像多模態融合的故障識別,適合研究學習(Matlab完整源碼和數據),附模型研究報告

基于MTF的1D-2D-CNN-GRU-Attention時序圖像多模態融合的故障識別&#xff0c;適合研究學習&#xff08;Matlab完整源碼和數據&#xff09;&#xff0c;附模型研究報告 目錄 基于MTF的1D-2D-CNN-GRU-Attention時序圖像多模態融合的故障識別&#xff0c;適合研究學習&#xff08;…

HTTP/1.1 隊頭堵塞問題

文章目錄 一、隊頭堵塞1、非管線化2、管線化 二、如何解決&#xff1f; 一、隊頭堵塞 1、非管線化 如圖&#xff0c;http 請求必須等到上一個請求響應后才能發送&#xff0c;后面的以此類推&#xff0c;由此可以看出&#xff0c;在一個 tcp 通道中&#xff0c;如果某個 http 請…

施磊老師基于muduo網絡庫的集群聊天服務器(二)

文章目錄 Cmake簡單介紹Cmake與MakefileCmake配置CmakeLists.txt 編寫完整cmake例子文件夾雜亂問題多級目錄Cmakevscode 極其推薦 的 cmake方式 Mysql環境與編程mysql簡單使用User表Friend表AllGroup表GroupUser表OfflineMessage表 集群聊天項目工程目錄創建網絡模塊代碼Chatse…

4.18---緩存相關問題(操作原子性,擊穿,穿透,雪崩,redis優勢)

為什么要用redis做一層緩存&#xff0c;相比直接查mysql有什么優勢&#xff1f; 首先介紹Mysql自帶緩存機制的問題&#xff1a; MySQL 的緩存機制存在一些限制和問題,它自身帶的緩存功能Query Cache只能緩存完全相同的查詢語句&#xff0c;對于稍有不同的查詢語句&#xff0c…

健康養生指南

在快節奏的現代生活中&#xff0c;健康養生成為人們關注的焦點。它不僅關乎身體的強健&#xff0c;更是提升生活質量、預防疾病的關鍵。掌握科學的養生方法&#xff0c;能讓我們在歲月流轉中始終保持活力。 飲食是健康養生的基礎。遵循 “均衡膳食” 原則&#xff0c;每日飲食需…

#去除知乎中“鹽選”付費故事

添加油猴腳本&#xff0c;去除知乎中“鹽選”付費故事 // UserScript // name 鹽選內容隱藏腳本 // namespace http://tampermonkey.net/ // version 0.2 // description 自動隱藏含有“鹽選專欄”或“鹽選”文字的回答卡片 // author YourName // mat…

如何防止接口被刷

目錄 &#x1f6e1;? 一、常見的防刷策略分類 &#x1f527; 二、技術實現細節 ? 1. 基于 IP 限流 ? 2. 給接口加驗證碼 ? 3. 使用 Token 限制接口訪問權限 ? 4. 給接口加冷卻時間&#xff08;驗證碼類經典&#xff09; ? 5. 使用滑動窗口限流算法&#xff08;更精…

github 項目遷移到 gitee

1. 查看遠程倉庫地址 git remote -v 2. 修改遠程倉庫地址 確保 origin 指向你的 Gitee 倉庫&#xff0c;如果不是&#xff0c;修改遠程地址。 git remote set-url origin https://gitee.com/***/project.git 3. 查看本地分支 git branch 4. 推送所有本地分支 git p…

探索大語言模型(LLM):目標、原理、挑戰與解決方案

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 前言語言模型的目標語言模型的數學表示語言模型面臨的挑戰解決參數量巨大的方法1. 馬爾可夫假設2. 神經網絡語言模型3.自監督學習4. 分布式表示 腦圖總結 前言 在自…

Kubernetes》》k8s》》Namespace

Namespace 概述 Namespace&#xff08;命名空間&#xff09; 是 Kubernetes 中用于邏輯隔離集群資源的機制&#xff0c;可將同一集群劃分為多個虛擬環境&#xff0c;適用于多團隊、多項目或多環境&#xff08;如開發、測試、生產&#xff09;的場景。 核心作用&#xff1a; 資…

FFUF指南

ffuf 的核心功能&#xff1a; 目錄/文件發現&#xff1a; 通過暴力破解&#xff08;使用字典&#xff09;探測目標網站的隱藏目錄或文件&#xff0c;例如&#xff1a; ffuf -w /path/to/wordlist.txt -u http://target.com/FUZZ 子域名枚舉&#xff1a; 通過模糊測試發現目標…

Qt通過ODBC和QPSQL兩種方式連接PostgreSQL或PolarDB PostgreSQL版

一、概述 以下主要在Windows下驗證連接PolarDB PostgreSQL版&#xff08;阿里云兼容 PostgreSQL的PolarDB版本&#xff09;。Linux下類似&#xff0c;ODBC方式則需要配置odbcinst.ini和odbc.ini。 二、代碼 以下為完整代碼&#xff0c;包含兩種方式連接數據庫&#xff0c;并…

為什么浮點數會搞出Infinity和NAN兩種類型?浮點數的底層原理?IEEE 754標準揭秘?

目錄 什么是NAN? 不同編程語言的NaN 為什么浮點數會搞出Infinity和NAN兩種類型? 浮點數 小數點位置浮動的原因 浮點數和整數 浮點數指令 精確性 浮點數的類型 為什么叫浮點數? 小數點位置浮動的原因 IEEE 754起源于intel公司 IEEE 754標準 編程語言的浮點數都…

Node.js Session 原理簡單介紹 + 示例代碼

目錄 ? Session 原理簡要說明 &#x1f9e9; 示例項目 - 使用 Node.js Express 實現簡單 Session 登錄 &#x1f4c1; 文件結構 &#x1f539; server.js (JavaScript) &#x1f538; index.html (HTML) ?? 程序運行步驟 ? 程序運行效果 &#x1f3af; 總結 在 We…

實戰交易策略 篇十六:獵豹阿杜打板交易策略

文章目錄 系列文章狙擊漲停板的十大要訣炒股大成者,必具“三商”系列文章 實戰交易策略 篇一:奧利弗瓦萊士短線交易策略 實戰交易策略 篇二:杰西利弗莫爾股票大作手操盤術策略 實戰交易策略 篇三:333交易策略 實戰交易策略 篇四:價值投資交易策略 實戰交易策略 篇五:底部…

Opentelemetry 項目解讀

Opentelemetry 解讀 1. 什么是 Opentelmetry Ot 統一了可觀測的三個重要維度&#xff1a;分別是 Trace&#xff0c;Log&#xff0c;Metrics。 在沒有 ot 之前&#xff0c;不同維度的可觀測組件都是不同的&#xff1a; 在 Trace 領域&#xff1a;skywalking 一直很受歡迎&am…

與終端同居日記:Linux指令の進階撩撥手冊

前情提要&#xff1a; 當你和終端的關系從「早安打卡」進階到「深夜代碼同居」&#xff0c;那些曾經高冷的指令開始展露致命の反差萌—— man 是那個永遠在線的鋼鐵直男說明書&#xff0c;只會說&#xff1a;"想懂我&#xff1f;自己看文檔&#xff01;"&#xff08…

Java 開發玩轉 MCP:從 Claude 自動化到 Spring AI Alibaba 生態整合

摘要 本文以原理與示例結合的形式講解 Java 開發者如何基于 Spring AI Alibaba 框架玩轉 MCP&#xff0c;主要包含以下內容。 1. 一些 MCP 基礎與快速體驗&#xff08;熟悉的讀者可以跳過此部分&#xff09; 2. 如何將自己開發的 Spring 應用發布為 MCP Server&#xff0c;驗…

【面試向】欠擬合和過擬合、正則化(Regularization)

訓練集、驗證集和測試集泛化誤差過擬合&#xff08;Overfitting&#xff09;和 欠擬合&#xff08;Underfitting&#xff09;如何區分過擬合和欠擬合&#xff1f;欠擬合 —— 在訓練集和驗證集上都表現很差過擬合 —— 在訓練集上表現很好&#xff0c;但在驗證集或測試集上表現…

ClawCloud的免費空間(github用戶登錄可以獲得$5元/月的免費額度)

免費的空間 Welcome to ClawCloud Lets create your workspace 官網&#xff1a;ClawCloud | Cloud Infrastructure And Platform for Developers 區域選擇新加坡 然后這個頁面會變成新加坡區域&#xff0c;再按一次確定&#xff0c;就創建好了工作臺。 初始界面&#xff0…