javascript - 未捕获的类型错误:需要库时无法读取未定义的属性“解析”
问题描述
我正在使用 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 行,错误就会消失,但我需要它。知道为什么我会收到此错误吗?在此先感谢您的帮助。
解决方案
您收到此错误是因为在文件docuvieware-min.js
中的行166
中,该eval
函数无法读取parse
不包含它的对象的属性。
更简洁:Javascript 期望
object = {
parse: function() {...}
};
你提供
object = undefined;
当您尝试将缩小文件导入非缩小项目时,这就是您所期望的。
要在打字稿中使用它,您需要导入定义文件。如果您不知道如何操作,则需要在文件中将文件声明为脚本angular-cli.json
。完成后,该对象将被声明为全局,因此您有权使用它
declare var docuVieware: any;
docuVieware.DocuViewareAPI.PostCustomServerAction(...)
推荐阅读
- java - Azure 发布管道未从 nestedStack.yml 文件中获取变量
- c++ - 替换 CComVariant 的低风险选项?
- angular7 - 带有签名功能的查看器的角度 7
- npm - 什么情况下需要NPX?
- python - 如何将数据库中的所有数据作为 Json 返回?
- c++ - 在构造函数中将全局类成员指针添加到 std::list 会导致下一个类添加时出现问题
- javascript - 如何添加这些依赖项?
- php - php版本及与Mysql 8的兼容性
- .htaccess - htaccess 如何设置诸如捕获所有指向一个 index.html 的链接?
- json - oracle - json 路径中的 & 符号