【问题标题】:EXCEPTION: mammoth is not defined例外:猛犸象未定义
【发布时间】:2017-11-24 07:55:41
【问题描述】:

我正在尝试将庞大的 npm 库与流星集成。

使用导入命令时, import { mammoth } from "mammoth"; 越来越

未捕获的类型错误:无法读取未定义的属性“绑定”

使用命令时, declare var mammoth: any; 越来越

例外:未定义猛犸象

我使用的代码是

this.readFileInputEventAsArrayBuffer(event, function(arrayBuffer) {
      mammoth
        .convertToHtml({ arrayBuffer: arrayBuffer })
        .then(function(result: any) {
          var html = result.value; // The generated HTML
          var messages = result.messages; // Any messages, such as warnings during conversion
          // console.log('html: ' + html);
          document.getElementById("output").innerHTML = html;
          var elements = document.getElementById("output").children;
          console.log(elements);
          console.log(JSON.stringify(elements));
          console.log(elements.length);
          for (var i = 0; i < elements.length; i++) {
            console.log(i + " --- ");
            console.log(elements[i]);
            var data = elements[i].innerHTML;
            // elements[i].setAttribute("draggable","true");
          }
        })
        .done();
      console.log(
        "event in fileUpload-readFileInputEventAsArrayBuffer" + event
      );
    });

 readFileInputEventAsArrayBuffer(event, callback) {
    var file = event.target.files[0];

    var reader = new FileReader();

    reader.onload = function(loadEvent: any) {
      var arrayBuffer = loadEvent.target.result;
      console.log("arrayBuffer: ");
      console.log(arrayBuffer);
      callback(arrayBuffer);
    };

    reader.readAsArrayBuffer(file);
  }

使用导入时

声明 var mammoth: any;

得到错误

ReferenceError: mammoth is not defined
at app.component.ts:75
at FileReader.reader.onload (app.component.ts:109)
at ZoneDelegate.invoke (modules.js:22723)
at Object.onInvoke (modules.js:57096)
at ZoneDelegate.invoke (modules.js:22722)
at Zone.runGuarded (modules.js:22607)
at FileReader.<anonymous> (modules.js:22585)

Uncaught ReferenceError: mammoth is not defined
at app.component.ts:75
at FileReader.reader.onload (app.component.ts:109)
at ZoneDelegate.invoke (modules.js:22723)
at Object.onInvoke (modules.js:57096)
at ZoneDelegate.invoke (modules.js:22722)
at Zone.runGuarded (modules.js:22607)
at FileReader.<anonymous> (modules.js:22585)

使用导入时

从“猛犸象”导入 { mammoth };

得到错误

Uncaught TypeError: Cannot read property 'bind' of undefined
at meteorInstall.node_modules.mammoth.lib.docx.files.js (modules.js?hash=cd1f432…:83272)
at fileEvaluate (modules-runtime.js?hash=637cb12…:191)
at require (modules-runtime.js?hash=637cb12…:116)
at meteorInstall.node_modules.mammoth.lib.docx.docx-reader.js (modules.js?hash=cd1f432…:82091)
at fileEvaluate (modules-runtime.js?hash=637cb12…:191)
at require (modules-runtime.js?hash=637cb12…:116)
at meteorInstall.node_modules.mammoth.lib.index.js (modules.js?hash=cd1f432…:81954)
at fileEvaluate (modules-runtime.js?hash=637cb12…:191)
at require (modules-runtime.js?hash=637cb12…:116)
at meteorInstall.client.imports.app.app.component.js (app.component.ts:12)
at fileEvaluate (modules-runtime.js?hash=637cb12…:191)
at require (modules-runtime.js?hash=637cb12…:116)
at meteorInstall.client.imports.app.index.js (index.ts:1)
at fileEvaluate (modules-runtime.js?hash=637cb12…:191)
at require (modules-runtime.js?hash=637cb12…:116)
at meteorInstall.client.main.js (main.ts:5)
at fileEvaluate (modules-runtime.js?hash=637cb12…:191)
at require (modules-runtime.js?hash=637cb12…:116)
at demo.collection.ts:4

请帮忙。

P.S.:作为 Angular 项目运行时,它运行良好。面对 问题,在 angular-meteor 项目中进行时。

【问题讨论】:

  • 您如何将庞大的库导入您的页面?
  • 试试import mammoth from "mammoth";?
  • @Alex,当我使用它导入时,得到这个 Uncaught TypeError: Cannot read property 'bind' of undefined。我的疑问是流星是否支持猛犸象库的集成/使用?
  • 请注意我的示例中没有 { 和 }
  • @Raven,我正在通过尝试以下操作导入庞大的库,import { mammoth } from "mammoth"; import mammoth from "mammoth"; declare var mammoth: any; var mammoth = require("mammoth");

标签: javascript meteor npm node-modules mammoth


【解决方案1】:

尝试像这样导入: import {convertToHtml} from 'mammoth/mammoth.browser';

在 componet.ts 文件中:

import {convertToHtml} from "mammoth/mammoth.browser";
                   

convertToHtml({ arrayBuffer: arrayBuffer })
.then(function(result){
    var html = result.value; // The generated HTML
    var messages = result.messages; // Any messages, such as warnings during conversion
})
.done();

以上代码在 Angular 9 中为我工作。

【讨论】:

    【解决方案2】:

    试试看这是否可行:import 'mammoth/mammoth.browser';(浏览器构建)

    或者,如果您可能想在前端执行此操作,那么您可以使用然后将以下脚本包含到您的 html 页面并在运行时执行此操作。 (将doc转换为html并渲染)

    要包含的脚本:

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      如果你查看 mammoth npm 包的源代码,你会发现没有 'mammoth' 也没有默认导出。但是,它确实会导出每个方法,因此您可以这样做:

      import {convertToHtml} from "mammoth";
      
      convertToHtml({path: "path/to/document.docx"})
          .then(function(result){
              var html = result.value; // The generated HTML 
              var messages = result.messages; // Any messages, such as warnings during conversion 
          })
          .done();
      

      这应该可以解决问题:)

      【讨论】:

      • 嗨 Mikkel,感谢您的回答。但即使如此,我也面临以下错误。 Uncaught TypeError: Cannot read property 'bind' of undefined at meteorInstall.node_modules.mammoth.lib.docx.files.js (modules.js?hash=ee5060b…:83272) at fileEvaluate (modules-runtime.js?hash=637cb12…:191) at require (modules-runtime.js?hash=637cb12…:116) at meteorInstall.node_modules.mammoth.lib.docx.docx-reader.js (modules.js?hash=ee5060b…:82091) at fileEvaluate (modules-runtime.js?hash=637cb12…:191) at require (modules-runtime.js?hash=637cb12…:116)
      • at meteorInstall.node_modules.mammoth.lib.index.js (modules.js?hash=ee5060b…:81954) at fileEvaluate (modules-runtime.js?hash=637cb12…:191) at require (modules-runtime.js?hash=637cb12…:116) at meteorInstall.client.imports.app.app.component.js (app.component.ts:3) at fileEvaluate (modules-runtime.js?hash=637cb12…:191) at require (modules-runtime.js?hash=637cb12…:116) at meteorInstall.client.imports.app.index.js (index.ts:1) at fileEvaluate (modules-runtime.js?hash=637cb12…:191) at require (modules-runtime.js?hash=637cb12…:116)
      • at meteorInstall.client.main.js (main.ts:5) at fileEvaluate (modules-runtime.js?hash=637cb12…:191) at require (modules-runtime.js?hash=637cb12…:116) at demo.collection.ts:4
      • 这方面的任何替代方法/帮助?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多