例外:猛犸象未定义
我正在尝试将 mammoth npm 库与 Meteor 集成。
使用 import 命令时, import { mammoth } from "mammoth"; 得到
Uncaught TypeError: Cannot read property 'bind' of undefined
使用命令时, declare var mammoth: any; 得到
EXCEPTION: mammoth is not defined
我使用的代码是
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);
}
使用 import 时
declare 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)
使用 import 时
import { mammoth } from “猛犸象”;
出现错误
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.: When running as a angular project, it is working fine. Facing the issue, when doing it in angular-meteor project.
尝试看看这是否有效:
import 'mammoth/mammoth.browser';
(浏览器构建)
或者,如果您可能希望在前端执行此操作,那么您可以使用以下脚本将其包含到您的 html 页面上并在运行时执行此操作。(将文档转换为 html 并渲染)
- 正如本文中所示 -> https://mike.zwobble.org/projects/mammoth/
要包含的脚本:
- demo.js -> https://mike.zwobble.org/static/projects/mammoth/demo.js
- mammoth.browser.min.js -> https://mike.zwobble.org/static/projects/mammoth/mammoth.browser.min.js
希望这对你有帮助。
如果您查看 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();
这应该可以解决问题 :)
尝试像这样导入:
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 中对我有用。