我正在使用 Angular 7 构建一个 web 应用程序,我尝试实现搜索功能,如 https://mozilla.github.io/pdf.js/web/viewer.html .
我有它的工作,但我喜欢在官方查看器中显示当前匹配和总匹配。我的代码如下所示:
this.pdfFindController.executeCommand('find', this.searchCommand);
let { current, total, } = this.pdfFindController._requestMatchesCount();
this.currentMatch = current;
this.numberOfSearchMatches = total;
console.log('Number of matches', this.currentMatch, '/',
this.numberOfSearchMatches);
当我运行此代码时,this.currentMatch 显示的是前一场比赛而不是当前比赛。如果我像这样修改代码:
this.pdfFindController.executeCommand('find', this.searchCommand);
setTimeout(() => {
let { current, total, } =
this.pdfFindController._requestMatchesCount();
this.currentMatch = current;
this.numberOfSearchMatches = total;
console.log('Number of matches', this.currentMatch, '/',
this.numberOfSearchMatches);
}, 1000);
那么 this.currentMatch 包含正确的匹配号。所以我想我需要在调用 executeCommand 和 _requestMatcheCount 之间等待一些东西。我已经广泛搜索了代码,但我还没有找到一种方法来知道 executeCommand 何时完成,因此可以安全地调用 _requestMatchesCount。谁能告诉我我怎么知道 executeCommand 已经完成?
最佳答案
我们来了,可能有点晚了!this.pdfComponent.pdfViewer.eventBus.on('updatefindmatchescount', data => {
this.searchMatchesLength = data.matchesCount.total;
});
关于pdf.js - 如何知道 pdfFindController.executeCommand 何时执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55128023/