更新记录
1.0.0(2025-05-23)
下载此版本
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:支持,iOS:支持,HarmonyNext:支持 |
× |
√ |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
× |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
kux-symbol
基于 uts
的 Symbol
实现, 提供跨平台唯一标识符解决方案。
特性
- 🚀 完全兼容 ES6 Symbol 核心特性
- ⚡ 支持全局符号注册表 (类似
Symbol.for()
/Symbol.keyFor()
)
- 🌐 内置异步迭代器支持 (Symbol.asyncIterator)
- 🔒 严格的类型安全校验
- 🛡️ 禁止隐式类型转换 (运算符重载保护)
快速开始
import { useSymbol } from '@/uni_modules/kux-symbol';
// 创建唯一符号
const sym1 = useSymbol('kux-button');
const sym2 = useSymbol('kux-button');
console.log(sym1 == sym2); // false
API文档
核心方法
方法 |
描述 |
KuxSymbol.create(desc?) |
创建新符号 (类似 Symbol() ) |
KuxSymbol.forKey(key) |
全局注册访问 (类似 Symbol.for() ) |
KuxSymbol.keyFor(sym) |
反向查询键名 (类似 Symbol.keyFor() ) |
内置符号
符号 |
协议 |
KuxSymbol.asyncIterator |
异步迭代器协议 |
KuxSymbol.hasInstance |
类型检查协议 |
接口定义
/**
* interface.uts
* uts插件接口定义文件,按规范定义接口文件可以在HBuilderX中更好的做到语法提示
*/
export interface IKuxSymbol {
// uniqueId: string
readonly description: string;
// asyncIterator: IKuxSymbol;
// hasInstance: IKuxSymbol;
// create(description?: string | null): IKuxSymbol;
// forKey(key: string): IKuxSymbol;
// keyFor(symbol: IKuxSymbol): string | null;
toString(): string;
getAsyncIterator(obj: UTSJSONObject): AsyncIterator | null;
checkInstance(obj: any): boolean;
// #ifndef APP-ANDROID
valueOf(): never;
// #endif
}
export type AsyncIteratorResult = {
value?: number | null;
done: boolean;
}
export type AsyncIterator = {
next(): Promise<AsyncIteratorResult>;
};
export type AsyncIterable = {
getAsyncIterator(): AsyncIterator;
}
export declare function useSymbol(description: string): IKuxSymbol;
最佳实践
跨组件符号共享
// ComponentA
const APP_SYMBOL = KuxSymbol.forKey('app.unique');
// ComponentB
const sameSymbol = KuxSymbol.forKey('app.unique');
console.log(APP_SYMBOL === sameSymbol); // true
结语
友情推荐