在 Angular/Ionic 中将字符串编码为 cp852 - iconv-lite
Encode string to cp852 in Angular/Ionic - iconv-lite
我正在开发移动应用程序,它使用蓝牙打印机打印账单。要使用正确的变音符号打印账单,我需要对其进行编码以更正 coddepage。这可以用这个 iconv-lite 库来完成,但我无法让它工作。有没有人使用过 iconv-lite 和 ionic?或者有没有其他方法可以用 ionic 编码字符串?
我成功安装了它,将它导入我的 printerService 并尝试 运行 ionic serve,当我尝试 运行 testEncoding 方法时出现以下错误,它使用 iconvlite:
core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'isEncoding' of undefined
TypeError: Cannot read property 'isEncoding' of undefined
at Object../node_modules/string_decoder/lib/string_decoder.js (string_decoder.js:29)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/iconv-lite/encodings/internal.js (internal.js:49)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/iconv-lite/encodings/index.js (index.js:6)
at __webpack_require__ (bootstrap:84)
at Object.getCodec (index.js:65)
at Object.getEncoder (index.js:117)
at Object.encode (index.js:22)
at PrintService.testEncode (print.service.ts:26)
at resolvePromise (zone-evergreen.js:797)
at zone-evergreen.js:862
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:469)
at invokeTask (zone-evergreen.js:1603)
at HTMLElement.globalZoneAwareCallback (zone-evergreen.js:1629)
我的打印服务中的代码如下所示
import { Injectable } from '@angular/core';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial/ngx';
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
import { switchMap, tap, catchError } from 'rxjs/operators';
import { from, throwError } from 'rxjs';
import * as iconvlite from 'iconv-lite';
@Injectable({
providedIn: 'root'
})
export class PrintService {
...
testEncoding(){
console.log(iconvlite.encode("testžýáč", "cp852"));
}
奇怪的是,即使我将 testEncoding 方法更改为
console.log(iconvlite.encodingExists("cp852"));
它 returns 编码无法识别错误,尽管编码名称是正确的。
即使我尝试使用此 ESC POS 离子库,我也会遇到相同的错误 - https://github.com/Ans0n-Ti0/EscPosEncoder(with 不同之处在于,即使我输入正确,我 运行 离子服务时的错误也会显示“未知代码页”代码页如 cp852)
知道问题出在哪里吗?我正在使用 ionic 5 和 angular 8。或者还有其他方法可以在 javascript 中将 utf8 字符串转换为 cp852 吗?我试图解决这个问题很长时间了,但我找不到解决方案。也许我需要在前端为 运行 iconvlite 安装一些额外的包?
似乎 angular 8 有一些问题,可能是它的 webpack 设置,当我升级到较新的版本时,它运行顺利。
我正在开发移动应用程序,它使用蓝牙打印机打印账单。要使用正确的变音符号打印账单,我需要对其进行编码以更正 coddepage。这可以用这个 iconv-lite 库来完成,但我无法让它工作。有没有人使用过 iconv-lite 和 ionic?或者有没有其他方法可以用 ionic 编码字符串?
我成功安装了它,将它导入我的 printerService 并尝试 运行 ionic serve,当我尝试 运行 testEncoding 方法时出现以下错误,它使用 iconvlite:
core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'isEncoding' of undefined
TypeError: Cannot read property 'isEncoding' of undefined
at Object../node_modules/string_decoder/lib/string_decoder.js (string_decoder.js:29)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/iconv-lite/encodings/internal.js (internal.js:49)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/iconv-lite/encodings/index.js (index.js:6)
at __webpack_require__ (bootstrap:84)
at Object.getCodec (index.js:65)
at Object.getEncoder (index.js:117)
at Object.encode (index.js:22)
at PrintService.testEncode (print.service.ts:26)
at resolvePromise (zone-evergreen.js:797)
at zone-evergreen.js:862
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:39680)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:469)
at invokeTask (zone-evergreen.js:1603)
at HTMLElement.globalZoneAwareCallback (zone-evergreen.js:1629)
我的打印服务中的代码如下所示
import { Injectable } from '@angular/core';
import { BluetoothSerial } from '@ionic-native/bluetooth-serial/ngx';
import { Storage } from '@ionic/storage';
import { Platform } from '@ionic/angular';
import { switchMap, tap, catchError } from 'rxjs/operators';
import { from, throwError } from 'rxjs';
import * as iconvlite from 'iconv-lite';
@Injectable({
providedIn: 'root'
})
export class PrintService {
...
testEncoding(){
console.log(iconvlite.encode("testžýáč", "cp852"));
}
奇怪的是,即使我将 testEncoding 方法更改为
console.log(iconvlite.encodingExists("cp852"));
它 returns 编码无法识别错误,尽管编码名称是正确的。
即使我尝试使用此 ESC POS 离子库,我也会遇到相同的错误 - https://github.com/Ans0n-Ti0/EscPosEncoder(with 不同之处在于,即使我输入正确,我 运行 离子服务时的错误也会显示“未知代码页”代码页如 cp852)
知道问题出在哪里吗?我正在使用 ionic 5 和 angular 8。或者还有其他方法可以在 javascript 中将 utf8 字符串转换为 cp852 吗?我试图解决这个问题很长时间了,但我找不到解决方案。也许我需要在前端为 运行 iconvlite 安装一些额外的包?
似乎 angular 8 有一些问题,可能是它的 webpack 设置,当我升级到较新的版本时,它运行顺利。