推薦使用條件編譯,少用條件判斷 // @/utils/EPlatform.ts/**枚舉EPlatform*/ export enum EPlatform { /**App*/ AppPlus = 'APP-PLUS', /**App nvue*/ AppPlusNvue = 'APP-PLUS-N ...
推薦使用條件編譯,少用條件判斷
// @/utils/EPlatform.ts
/**枚舉EPlatform*/
export enum EPlatform { /**App*/ AppPlus = 'APP-PLUS', /**App nvue*/ AppPlusNvue = 'APP-PLUS-NVUE', /**H5*/ H5 = 'H5', /**微信小程式*/ MpWeixin = 'MP-WEIXIN', /**支付寶小程式*/ MpAlipay = 'MP-ALIPAY', /**百度小程式*/ MpBaidu = 'MP-BAIDU', /**位元組跳動小程式*/ MpToutiao = 'MP-TOUTIAO', /**QQ小程式*/ MpQq = 'MP-QQ', /**360小程式*/ Mp360 = 'MP-360', /**微信小程式/支付寶小程式/百度小程式/位元組跳動小程式/QQ小程式/360小程式*/ Mp = 'MP', /**快應用通用(包含聯盟、華為)*/ QuickappWebview = 'quickapp-webview', /**快應用聯盟*/ QuickappWebviewUnion = 'quickapp-webview-union', /**快應用華為*/ QuickappWebviewHuawei = 'quickapp-webview-huawei', }
// @/utils/Platform.ts
/**類型Platform*/ import {EPlatform} from "@/utils/EPlatform"; /**使用條件編譯獲取平臺信息*/ function ifDefPlatform(): EPlatform { let platform: EPlatform //#ifdef APP-PLUS platform = EPlatform.AppPlus; //#endif //#ifdef APP-PLUS-NVUE platform = EPlatform.AppPlusNvue; //#endif //#ifdef H5 platform = EPlatform.H5; //#endif //#ifdef MP-WEIXIN platform = EPlatform.MpWeixin; //#endif //#ifdef MP-ALIPAY platform = EPlatform.MpAlipay; //#endif //#ifdef MP-BAIDU platform = EPlatform.MpBaidu; //#endif //#ifdef MP-TOUTIAO platform = EPlatform.MpToutiao; //#endif //#ifdef MP-QQ platform = EPlatform.MpQq; //#endif //#ifdef MP-360 platform = EPlatform.Mp360; //#endif //#ifdef MP platform = EPlatform.Mp; //#endif //#ifdef quickapp-webview platform = EPlatform.QuickappWebview; //#endif //#ifdef quickapp-webview-union platform = EPlatform.QuickappWebviewUnion; //#endif //#ifdef quickapp-webview-huawei platform = EPlatform.QuickappWebviewHuawei; //#endif return platform } /**平臺類型*/ export const Platform: EPlatform = ifDefPlatform() /**預設導出平臺類型*/ export default Platform /**App*/ export const isAppPlus = Platform == EPlatform.AppPlus /**App nvue*/ export const isAppPlusNvue = Platform == EPlatform.AppPlusNvue /**H5*/ export const isH5 = Platform == EPlatform.H5 /**微信小程式*/ export const isMpWeixin = Platform == EPlatform.MpWeixin /**支付寶小程式*/ export const isMpAlipay = Platform == EPlatform.MpAlipay /**百度小程式*/ export const isMpBaidu = Platform == EPlatform.MpBaidu /**位元組跳動小程式*/ export const isMpToutiao = Platform == EPlatform.MpToutiao /**QQ小程式*/ export const isMpQq = Platform == EPlatform.MpQq /**360小程式*/ export const isMp360 = Platform == EPlatform.Mp360 /**微信小程式/支付寶小程式/百度小程式/位元組跳動小程式/QQ小程式/360小程式*/ export const isMp = Platform == EPlatform.Mp /**快應用通用(包含聯盟、華為)*/ export const isQuickappWebview = Platform == EPlatform.QuickappWebview /**快應用聯盟*/ export const isQuickappWebviewUnion = Platform == EPlatform.QuickappWebviewUnion /**快應用華為*/ export const isQuickappWebviewHuawei = Platform == EPlatform.QuickappWebviewHuawei
使用
// @/utils/Config.ts
import {EPlatform} from './EPlatform'; import {isH5, Platform} from '@/utils/Platform' /**配置信息*/ export default class Config { /**http請求根目錄*/ static get httpBaseUrl(): string { if (isH5) { return '/' } else { return 'http://demo.cn/' } } }
本文地址:
https://www.cnblogs.com/stumpx/p/13289154.html