我正在尝试使用 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 编译阶段它会抛出错误。我该如何处理?

最佳答案

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

 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;
  }

10-08 06:17