gy-roslib
Version:
gy
75 lines (55 loc) • 2.87 kB
Markdown
# gy-roslib
使用vue3.0+vite+ts封装的roslib组件
该组件支持VUE2.0以及VUE3.0,并针对ES5环境进行了BigInt兼容性处理。
文档地址:[https://guoyao132.github.io/npm-main/#/gy-roslib](https://guoyao132.github.io/npm-main/#/gy-roslib)
# 安装
使用 npm 安装。
```javascript
npm install gy-roslib --save
```
# BigInt ES5兼容性说明
该库内部使用了BigInt进行大数处理,为了确保在不支持BigInt的ES5环境(如IE11)中正常运行,我们采取了以下兼容性处理:
1. **big-integer库polyfill**:使用`big-integer`库作为BigInt的polyfill
2. **构建时转换处理**:在构建过程中自动将以下类型的BigInt字面量转换为兼容格式:
- 十进制BigInt(如`123n` → `BigInt('123')`)
- 十六进制BigInt(如`0x7fffffffffffffffn` → `BigInt('0x7fffffffffffffff')`)
- 负数值(如`-123n` → `BigInt('-123')`,`-0x7fffffffffffffffn` → `BigInt('-0x7fffffffffffffff')`)
- 带位运算符的表达式(如`~123n` → `BigInt('123').not()`,`~0x7fffffffffffffffn` → `BigInt('0x7fffffffffffffff').not()`)
3. **位运算符支持**:为polyfill的BigInt对象添加了`not()`方法,用于支持`~`按位非运算符
## 对于使用该库的项目
如果您的项目需要在不支持BigInt的环境中运行,建议在项目中添加以下配置:
```javascript
// webpack.config.js 或 vite.config.js
module.exports = {
// ...其他配置
resolve: {
alias: {
// 确保big-integer库被正确解析
'big-integer': require.resolve('big-integer')
}
}
}
```
## 常见问题
**问:在IE11中使用该库出现"BigInt is not defined"错误怎么办?**
**答:** 确保您的项目正确安装了big-integer依赖,并且在入口文件中引入了该库:
```javascript
// 在项目入口文件中添加
if (typeof BigInt !== 'function') {
try {
const bigInt = require('big-integer');
window.BigInt = bigInt;
// 添加位运算符支持
if (typeof BigInt.prototype.not !== 'function') {
BigInt.prototype.not = function() { return this.not(); };
}
} catch (e) {
console.warn('无法加载big-integer库,某些功能可能受限');
}
}
// 如果使用的是十六进制BigInt字面量(如0x7fffffffffffffffn),确保以下代码也被正确转换
// 转换前: return parseBigIntLiteral(str, ~0x7fffffffffffffffn, 0x7fffffffffffffffn);
// 转换后: return parseBigIntLiteral(str, BigInt('0x7fffffffffffffff').not(), BigInt('0x7fffffffffffffff'));
```
## 修复说明
我们已经修复了正则表达式处理十六进制BigInt字面量的问题,并创建了专门测试十六进制BigInt字面量的测试文件,确保在各种情况下都能正确转换和处理十六进制BigInt值。