首页 > 解决方案 > 未捕获的类型错误:需要库时无法读取未定义的属性“解析”

问题描述

我正在使用 Angular 5,我正在尝试使用一个 javascript 文件,该文件包含一些使用来自 docuvieware 的库的方法。我最终使用了 javascript,因为我无法在 typescript 上使用它。所以我有以下代码:

const { docuVieware } = require('../../../docuvieware/docuvieware-min');

module.exports = { loadFile: 
    function loadFile(file) {
    if (file != null) {
        var params = {
            Value: file,
            Example: false
        };
        docuVieware.DocuViewareAPI.PostCustomServerAction("DocuVieware1", 
 true, "loadFile", params, function(result){ console.log("result: " + 
 JSON.stringify(result)); });
    }
  }
};

但是每当我打开浏览器时,都会出现以下错误:

> docuvieware-min.js:166 Uncaught TypeError: Cannot read property 'parse' of undefined
    at eval (docuvieware-min.js:166)
    at eval (docuvieware-min.js:20)
    at Object.eval (docuvieware-min.js:20)
    at eval (docuvieware-min.js:1128)
    at Object../src/docuvieware/docuvieware-min.js (main.bundle.js:208)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (docuvieware.js:1)
    at Object../src/app/components/detalleExpediente/docuvieware.js (main.bundle.js:128)
    at __webpack_require__ (inline.bundle.js:55)
    at eval (detalleExpediente.component.ts:7)

所以,如果我评论 require 行,错误就会消失,但我需要它。知道为什么我会收到此错误吗?在此先感谢您的帮助。

标签: javascriptangular

解决方案


您收到此错误是因为在文件docuvieware-min.js中的行166中,该eval函数无法读取parse不包含它的对象的属性。

更简洁:Javascript 期望

object = {
  parse: function() {...}
};

你提供

object = undefined;

当您尝试将缩小文件导入非缩小项目时,这就是您所期望的。

要在打字稿中使用它,您需要导入定义文件。如果您不知道如何操作,则需要在文件中将文件声明为脚本angular-cli.json。完成后,该对象将被声明为全局,因此您有权使用它

declare var docuVieware: any;

docuVieware.DocuViewareAPI.PostCustomServerAction(...)

推荐阅读