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

概述

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

functionWechat.js

function method(){}
export {method}

functionUniapp.js

function method(){}
export {method}

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. 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田秋浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值