错误:未捕获(承诺):ReferenceError:未定义缓冲区

时间:2019-07-29 18:15:24

标签: javascript angular ionic4

我尝试在ionic 4(Angular)项目中使用可变缓冲区进行蓝牙打印。每次我尝试实例化MutableBuffer对象,如下所示。

我想使用Mutable缓冲区创建打印缓冲区对象,如下面的代码所示

import {MutableBuffer} from 'mutable-buffer';



@Component({
  selector: 'app-item',
  templateUrl: './item.page.html',
  styleUrls: ['./item.page.scss'],
})
export class ItemPage implements OnInit {

  buff = new MutableBuffer(10, 17);

}

  sendPrintCommandToBluetoothPrinter() {

      this.buff.write('\x1b\x61\x00');
      this.buff.write("Bill Date   ");


      this.buff.write('\x1b\x61\x02');
      this.buff.write("29-07-2019");//to the right
      this.buff.write('\x0a');

       this.buff.write('\x1b\x61\x00');
      this.buff.write("(Inc Tax)");//to the left
       this.buff.write('\x0a');

    this.PrintViaBluetooth(this.buff.buffer);


}

我收到以下错误:

core.js:15724 ERROR Error: Uncaught (in promise): ReferenceError: Buffer is not defined
ReferenceError: Buffer is not defined
    at new MutableBuffer (mutable-buffer.js:21)
    at new ItemPage (cart.page.ts:24)
    at createClass (core.js:22160)
    at createDirectiveInstance (core.js:22029)
    at createViewNodes (core.js:23255)
    at createRootView (core.js:23169)
    at callWithDebugContext (core.js:24177)
    at Object.debugCreateRootView [as createRootView] (core.js:23687)
    at ComponentFactory_.push../node_modules/@angular/core/fesm5/core.js.ComponentFactory_.create (core.js:21508)
    at ComponentFactoryBoundToModule.push../node_modules/@angular/core/fesm5/core.js.ComponentFactoryBoundToModule.create (core.js:9935)
    at resolvePromise (zone.js:831)
    at zone.js:741
    at rejected (tslib.es6.js:69)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:391)
    at Object.onInvoke (core.js:17299)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (zone.js:390)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (zone.js:150)
    at zone.js:889
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:17290)

1 个答案:

答案 0 :(得分:0)

mutable-buffer是一个NodeJS库,假设您的代码在NodeJS运行时中执行,它可以自由利用NodeJS的Buffer类。

Ionic是一个混合框架,您的代码正在WebView(本质上是浏览器)中执行,其中Buffer本身不可用。

尝试使用Buffer填充https://www.npmjs.com/package/buffer#usage

var Buffer = require('buffer/').Buffer  // note: the trailing slash is important!