Vue: use a custom libary (pdf.js) in a component

萝らか妹 提交于 2020-01-13 06:49:14

问题


How can I use a vendor libary (specifically I want to use PDF.js) in a Vue component? (I only want to load it for this specific component as they are rather larger files)

I'm building an editor that needs to load a pdf. So I placed the pdf.js and pdf.worker.js in /src/assets/vendor/pdfjs

Then I load both in the template-editor-page.hbs that also loads the component:

<div class="content">
  <div class="row fill">
    <div class="col-md-2 fill br pt30">
    </div>
    <div class="col-md-10 fill pt30 pl30 pr30">
      <div id="template-editor" class="template-editor">  
        <template-editor template-src="{{template.src}}"></template-editor>    
      </div>
    </div>
  </div>
</div>
<script src="/assets/js/template-editor.bundle.js"></script>
<script src="/assets/vendor/pdfjs/pdf.js"></script>
<script src="/assets/vendor/pdfjs/pdf.worker.js"></script>

my template-editor.js (do I have to load it here?):

import Vue from 'vue';
import templateEditor from './components/template-editor.vue';

new Vue({
  el: '#template-editor',
  components: { templateEditor }
});

Now I want to load the file in my template-editor.vue:

<template>
    <!-- ... -->
</template>

<script>

  export default {
    props: ['templateSrc'],
    name: 'template-editor',
    data() {
      return {
        src: this.templateSrc
      };
    },
    methods: {
      render() {
        PDFJS.getDocument(this.$data.src).then(function(pdf) {
          console.log(pdf);
        }, err => console.log(err));
      }
    },
    created: function() {
      this.render();
    }
  };
</script>

But I get an error saying

ReferenceError: PDFJS is not defined

Everything else seems to be working out fine. What am I missing?


回答1:


I think all that's missing is an import statement in your component,

CORRECTION Try with an '@' in the import location below. I forgot, your component is probably in a sub-folder of 'src'. Also see note below about pdfjs-dist.

<script>
  import { PDFJS } from '@/assets/vendor/pdfjs/pdf.js'

  export default {
    props: ['templateSrc'],
    name: 'template-editor',
    ...

Alternative

Since you have webpack, you might be better off installing pdfjs-dist into node modules (see pdfjs-dist), and removing it from './assets/vendor/pdfjs/pdj.js'

npm install pdfjs-dist

If you do this, the import is more 'standard',

import { PDFJS } from 'pdfjs-dist'



回答2:


Instead of script tags for your vendor scripts, better use webpacks dynamic import feature (https://webpack.js.org/guides/code-splitting/#dynamic-imports) to load this vendor library in your render function:

render() {
    import('/assets/vendor/pdfjs/pdf.js').then(PDFJS => {
        PDFJS.getDocument(this.$data.src).then(function(pdf) {
          console.log(pdf);
        }, err => console.log(err));
    }
}

For import to work you will also have to install this babel plugin http://babeljs.io/docs/plugins/syntax-dynamic-import/.




回答3:


Thank's for your help guys. Turns out the answer was hidden in the first snippet: I import the pdfjs AFTER the bundle. But since the bundle needs it, I need to import it BEFORE:

<script src="/assets/vendor/pdfjs/pdf.js"></script>
<script src="/assets/vendor/pdfjs/pdf.worker.js"></script>
<script src="/assets/js/template-editor.bundle.js"></script>

Really not all that complicated after all ;)



来源:https://stackoverflow.com/questions/46579229/vue-use-a-custom-libary-pdf-js-in-a-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!