TypeError:无法从 firefox 开发的 pdf js 中的私有字段读取

问题描述 投票:0回答:1

我正在使用 pdfjs 在模式中显示 pdf,并且我正在使用 alpine js 来实现此功能

pdf 有多个文件,我想在 pdf 顶部显示导航按钮,以便用户可以导航,但出现此错误:

未捕获(承诺中)类型错误:无法从私有字段读取

我愿意接受任何想法
预先感谢

这是我的代码:

pdfInstance: false,

   createPdf() {
        PDFJS.getDocument(option.url).promise.then(pdf => {
            this.pdfInstance = pdf;
            this.renderPage(1);
        });
    },
    
    renderPage(page) {

        this.pdfInstance.getPage(page).then(page => {

               console.log(page)
        })

    }

我希望看到 pdf 文件,但总是收到此错误。如果我不将 pdf 分配给 pdfInstance,它可以正常工作,但是我想渲染另一个页面,所以我必须每次都创建文档,我不知道这是否是一个好主意

此代码有效,但我认为它无效,因为每次渲染新页面时我都必须创建文档

 createPdf(page) {

        PDFJS.getDocument(option.url).promise.then(pdf => {
          
             pdf.getPage(page).then(page => {

               console.log(page)
             })
           
            this.renderPage(1);
        });
    },
   
javascript pdf.js alpine.js pdfjs-dist
1个回答
0
投票

嗨,把这个留给有同样问题的人

事实证明,AlpineJs 的行为是将任何数据属性转换为代理,以便使其具有反应性,因此如果您希望任何 alpinejs 属性不具有反应性,您可以将它们声明为变量,而不是像这样声明为 alpine 数据属性

export default (option) => {
    let pdfInstance;
  
     return { 
        createPdf() {
            PDFJS.getDocument(option.url).promise.then(pdf => {
                pdfInstance = pdf; 
                this.renderPage(1);
            });
        },

        renderPage(pageNumber) {
            pdfInstance.getPage(pageNumber)

        }

     }

   }

© www.soinside.com 2019 - 2024. All rights reserved.