【问题标题】:[TypeError: Cannot read property 'GlobalWorkerOptions' of undefined][TypeError: Cannot read property 'GlobalWorkerOptions' of undefined]
【发布时间】:2021-06-25 03:16:43
【问题描述】:

我的pdfjs-dist版本:"pdfjs-dist": "^2.4.456" 这是导入我的代码:

import PDFJS from 'pdfjs-dist'
import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'

当我打电话时:

PDFJS.GlobalWorkerOptions.workerSrc = pdfjsWorker
this.loadingTask = PDFJS.getDocument({ data: self.pdfData })

返回此错误:[TypeError: Cannot read property 'GlobalWorkerOptions' of undefined]

我试着安慰这个:PDFJS,它返回undefinded

所以当我在互联网上尝试这个解决方案时,我添加了

<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.5.207/pdf.min.js"></script>

在我的index.htmladd var PDFJS = window['pdfjs-dist/build/pdf'];

然后它完美地工作......但我不知道为什么第一种方法对我不起作用......而且我不想从 CDN 使用它,因为我认为它对我的项目来说不稳定。 那么你们能告诉我我在这里错过了什么吗?我试图在node-modules 中环顾四周以找出答案,但我仍然不知道为什么我不能import PDFJS from 'pdfjs-dist'

【问题讨论】:

    标签: javascript vue.js npm pdfjs-dist


    【解决方案1】:

    我解决了我的问题 我不知道这是否是问题的根源,但我只是简单地从 import PDFJS from 'pdfjs-dist'import * as PDFJS from 'pdfjs-dist' ,它工作得很好。

    【讨论】:

    • 原因是他们没有export default,所以你不能使用import PDFJS from 'pdfjs-dist'
    猜你喜欢
    • 2017-04-24
    • 2023-03-15
    • 2021-09-18
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2019-02-12
    • 2013-01-15
    相关资源
    最近更新 更多