• 基于UNI-APP实现适配器并保证适配器和实现的调用一致


    概述

    1. 前端功能的实现是基于不同的环境采用不同的实现方式的。
    2. 一种是企业微信小程序,需要基于企业微信框架实现。
    3. 一种是移动APP,需要基于uni-app的中底层实现。
    4. 为了调用方便,需要将两种实现统一在一种适配器中,调用者只需要指定环境是什么不需要选择方法。

    functionWechat.js

    function method(){}
    export {method}
    
    • 1
    • 2

    functionUniapp.js

    function method(){}
    export {method}
    
    • 1
    • 2

    functionAdapter.js

    import store from "./store";
    
    let type = store.getters.type;
    
    if (!type) {
    	type= "EnterpriseWeChat";
    }
    
    const weChatApi = require("./functionWechat.js"); // A
    const uniAppApi = require("./functionUniapp.js"); // A
    
    let adapter = {};
    
    switch (bluetoothType) {
    	case 'EnterpriseWeChat':
    		adapter = weChatBluetoothApi;
    		break;
    	case 'Uni-App':
    		adapter = uniAppBluetoothApi;
    		break;
    	default:
    }
    
    export const method= adapter.method;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    说明

    1. A处,命名导出和默认导出
    • ​​export { method }​ 是命名导出(Named Export),用于导出模块中的特定标识符,比如变量、函数或类。使用该语法时,你可以在导入时通过名称来引用这些标识符,例如 ​import { method } from './module.js’​。
    • ​​export default { method: method }​ 是默认导出(Default Export),用于导出模块中的一个默认值。默认导出可以是任何 JavaScript 数据类型,如对象、函数、类等。使用该语法时,你可以在导入时给默认导出一个任意的名称,例如使用 ​import myModule from './module.js’​ 来引入默认导出的模块。
    • 需要注意的是,一个模块中只能使用一次默认导出,而命名导出可以使用多次。另外,在导入模块时,命名导出需要使用大括号 ​{}​ 包裹要导入的标识符,而默认导出不需要。
    • 这里如果使用import引入,上下两处都需要使用import {method} from './functionWechat.js',这样会出现变量名重复,所以使用require。
    • 如果不是uni-app环境的话,可以尝试使用import {method} as weChat from './functionWechat'.js
  • 相关阅读:
    基于机器学习之模型树短期负荷预测(Matlab代码实现)
    硬件总线基础07:PCIe总线基础-事务层(1)
    vue 样式加scoped不起作用 局部更改element-ui的默认样式
    华为OD机试真题-找数字-2024年OD统一考试(C卷D卷)
    createSocketTask:fail wcwss url not in domain list 小程序网络异常
    【Java】Groovy 语言应用场景以及积累
    【C++ 学习】链接、作用域、内存管理
    提升爬虫IP时效:解决被封IP的难题
    css实现箭头
    CentOS ARM 部署 kubernetes v1.24.6
  • 原文地址:https://blog.csdn.net/tianqiuhao/article/details/134269942