如何在Angular 4项目中使用ActiveXObject

时间:2018-01-24 14:14:40

标签: angular typescript activexobject

我正在尝试使用下面的ActiveXObject

getActiveXObject(pdfCtrl) {
      return new ActiveXObject(pdfCtrl);
    }

    checkPDF() {
        let plugin = null;
            if (this.getBrowser() === 'ie') {
                plugin = this.getActiveXObject('AcroPDF.PDF') || this.getActiveXObject('PDF.PdfCtrl');
        }
        return plugin;
    }

它给出了像ActiveXObject未找到的错误。在普通的JS中,这是有效的,但在Angular / typesript编译阶段,它会抛出错误。我该如何处理?

1 个答案:

答案 0 :(得分:0)

msxml的示例-ActiveXObject仅在IE中使用本机方法在Chrome中可用:

 import * as ts from "typescript";
 ...
  private loadMsXml(src: string): any {
    let code: string = `({
      Run: (data: string): any => {
        let doc = new ActiveXObject("Msxml2.DOMDocument.6.0");
        doc.async = false;
        doc.loadXML(data);
        return doc;
      })`;
    const e = eval;
    let result = ts.transpile(code);
    let runnalbe: any = e(result);
    return runnalbe.Run(src);
  }

  private transformXml(xmlDoc: any, xsltDoc: any): string {
    if (this.isBrowserIE()) {
      let xmlDocMS = this.loadMsXml(new XMLSerializer().serializeToString(xmlDoc));
      let xslDocMS = this.loadMsXml(new XMLSerializer().serializeToString(xsltDoc));
      let resultXml = xmlDocMS.transformNode(xslDocMS);
      return resultXml;
    }

    let xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xsltDoc);
    let resultDoc = xsltProcessor.transformToDocument(xmlDoc);
    let resultXml = new XMLSerializer().serializeToString(resultDoc);
    return resultXml;
  }