0

if I register a component using async loading:

components: {
    pdf: () => import('vue-pdf'),
},

How can I access pdf (and pdf's methods) in the importing component?

Unfortunately it is not possible to just import the component and use it in components later.

4
  • What do you need to access on it? Commented Dec 20, 2020 at 22:52
  • I need to access a method of this component. Commented Dec 21, 2020 at 7:44
  • 1
    Could you give more context? What is the method, how is it used etc. There's likely a better way around this than accessing another component's method directly. Commented Dec 21, 2020 at 7:59
  • Normally you access a component's method through an instance of that component. You are trying to access the class method which will have no component this context and is probably not what you want. Please explain the details of what you are trying to do Commented Dec 21, 2020 at 13:15

1 Answer 1

1

You could use a template ref on the pdf component (e.g., named myPdf), and access the vue-pdf component's methods in your script via $refs.myPdf.

This example adds a button to print the first page of the PDF:

<template>
  <div>
    <pdf ref="myPdf" src="/tracemonkey.pdf" />
    <button @click="printFirstPage">Print page 1</button>
  </div>
</template>

<script>
export default {
  components: {
    pdf: () => import('vue-pdf')
  },
  methods: {
    printFirstPage() {
      const DPI = 150
      const pages = [1]
      this.$refs.myPdf.print(dpi, pages)
    }
  }
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.