Angular POS Print Issue

2019-07-30 18:48发布

My requirement: Print without print preview angular 6

Only solution i found

Angular 2 Raw Printing Service I am using think link for Angular POS print

Do i have any other alternatives?

.ts code

 printInvoice() {
  console.log(this.printService.getPrinters());
  }

My Service code

import { Observable } from 'rxjs';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { Injectable } from '@angular/core';
import * as shajs from 'sha.js';
import * as qz from 'qz-tray';
**import * as RSVP from 'rsvp';**

@Injectable()
export class QzTrayService {
    constructor() { }

    errorHandler(error: any): Observable<any> {
        console.log('error handler');
        return Observable.throw(error);
    }

    // Get list of printers connected
    getPrinters(): Observable<string[]> {
        console.log(Observable
            .fromPromise(qz.websocket.connect()
                .then(() => qz.printers.find()))
            .map((printers: string[]) => printers) );
        return Observable
            .fromPromise(qz.websocket.connect()
            .then(() => qz.printers.find()))
            .map((printers: string[]) => printers)
            .catch(this.errorHandler);
    }

    // Get the SPECIFIC connected printer
    getPrinter(printerName: string): Observable<string> {
        return Observable
            .fromPromise(qz.websocket.connect().then(() => qz.printers.find(printerName)))
            .map((printer: string) => printer)
            .catch(this.errorHandler);
    }

    // Print data to chosen printer
    printData(printer: string, data: any): Observable<any> {
        // Create a default config for the found printer
        const config = qz.configs.create(printer);
        return Observable.fromPromise(qz.print(config, data))
            .map((anything: any) => anything)
            .catch(this.errorHandler);
    }

    // Disconnect QZ Tray from the browser
    removePrinter(): void {
        qz.websocket.disconnect();
    }
}

Error:

I get RSVP is not defined, how to correct this?

enter image description here

1条回答
等我变得足够好
2楼-- · 2019-07-30 19:11

Adding it in to that file will not make it available to other scripts.

You need to include it as a global script.

To do so, add the following into your index.html:

<head>
   <script src="../node_modules/rsvp/dist/rsvp.min.js"></script>
</head> 

Alternately, you can include it in angular.json in the scripts section

查看更多
登录 后发表回答