概述
- 前端功能的实现是基于不同的环境采用不同的实现方式的。
- 一种是企业微信小程序,需要基于企业微信框架实现。
- 一种是移动APP,需要基于uni-app的中底层实现。
- 为了调用方便,需要将两种实现统一在一种适配器中,调用者只需要指定环境是什么不需要选择方法。
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");
const uniAppApi = require("./functionUniapp.js");
let adapter = {};
switch (bluetoothType) {
case 'EnterpriseWeChat':
adapter = weChatBluetoothApi;
break;
case 'Uni-App':
adapter = uniAppBluetoothApi;
break;
default:
}
export const method= adapter.method;
说明
- 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
。