Storing a Class Instance in Vue Data

I am currently incorporating PDFtron into a Vue component. To display the PDF viewer iFrame upon loading the Vue, I am initializing the instance in the mounted hook:

mounted() {
    const viewerElement = document.getElementById('viewer');
    this.viewer = new PDFTron.WebViewer({
      path: 'https://www.pdftron.com/4.0/lib',
      l: 'apikey'
    }, viewerElement);
  }

I want to retain this instance so that I can use it again in a method like so:

 methods: {
    getPDF() {
      this.viewer.loadDocument('https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo.pdf')
    }
  }

To achieve this, I tried creating a viewer variable within my data attributes and saving the pdftron viewer to it by storing it in this.viewer. However, whenever I attempt to call the getPDF function, I encounter the following error:

The viewer instance is not defined yet.
I'm unsure if this is the correct way to save a class instance in Vue.

The getPDF function triggers on a button click like this:

<v-btn color="primary" @click="getPDF(url)" :disabled="!valid">Load PDF</v-btn>
'

Update:

I revised my getPDF function as follows:

getPDF() {
      const viewerInstance = this.viewer.getInstance()
      viewerInstance.loadDocument('https://pdftron.s3.amazonaws.com/downloads/pl/webviewer-demo.pdf')
    }

Yet, I still encounter the same error message

The viewer instance is not defined yet
and `Cannot read property loadDocument of undefined'

Answer №1

It seems that the button click is being triggered before WebViewer has finished loading and initializing.

Remember, you won't be able to interact with the WebViewer API (aside from the constructor) until the ready event is fired.

Check out this page for a helpful example.

To ensure proper functionality, it's recommended to set up your button listeners within the ready event listener, allowing users to utilize those buttons only after the viewer is fully loaded.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoiding Overlapping in Recharts Scatter Plot

I am encountering an issue where the data I'm inputting into recharts is not overlapping, instead it is being stacked horizontally. This "stacked" data all has the same date on the x-axis and the same count on the y-axis. What I actually want is for t ...

Interactive Thumbnail Previews

There seems to be an issue with the thumbnail links on my page. The leftmost and rightmost links work fine, but the middle ones are not functioning properly when clicked. The PHP code used to generate these links is the same for all of them, so it's p ...

Steps for rearranging the order of CSS grid layout

Could you assist me in implementing a grid layout that fulfills these specific criteria? I'm unsure of how to proceed, so any guidance would be greatly appreciated. https://i.sstatic.net/vBtXc.png When there are more than 9 items, the current layout ...

In Vue, you can dynamically load a JavaScript file containing a JavaScript object during runtime

I'm in the process of developing a no-code application with Vue. I have come across an issue where I cannot add functions to a JSON file that I want to import at runtime. As a workaround, I decided to use a JavaScript or TypeScript file to store the J ...

Struggling to implement Google OAuth in a MERN stack - facing a 400 bad request error with the package @react-oauth/google

Here is the React form and relevant code sections for the issue: import { useGoogleLogin } from '@react-oauth/google'; const SignUpForm = () => { const navigate = useNavigate(); const [name, setName] = useState(""); const [email, setEm ...

How to access a template variable within a component

I encountered a problem with ng-template. In our table component, we pass parameters using ngTemplateOutletContext as shown below. Depending on the data type, we use different renderers for the cells. Pay attention to the cellvalue parameter, it is crucia ...

Maximize Rotation - JavaScript Rotation

Currently tackling a Codewars challenge. The task involves finding the maximum possible result after performing a rotation on a given number. This rotation is unique in that 'n' number of digits will remain fixed after each rotation, with &apos ...

Tips for highlighting a button within a details list using React and Office Fabric

Does anyone have any tips on how to focus a button in React DetailsList? I've been considering using refs and attempting something like this: findDOMNode<HTMLButtonElement>(this.refs.mybutton).focus() However, I've encountered dif ...

What's preventing me from changing module.exports? Why is my browser suddenly giving me errors after `npm run build` ran smoothly?

How come the browser is preventing me from executing the following code: Getters.js let Getters = {} Getters.foo = function(){ return 1; } Getters.bar = function(){ return 2; } module.exports = Getters; However, after running npm run build and serve -s ...

Discovering techniques to access nested objects in JavaScript

My initial object state looks like this: answersCount: { Colors: { Green: 0, Brown: 0, Blue: 0, Red: 0 }, Letters: { A: 0, B: 0, C: 0, D: 0, }, Briggs: { EI: 0, SN: 0, T ...

Validating Angular for controls that are not in a form

I am working on validating a form using reactive form group for non-form controls. As part of my form, I am incorporating custom components. Take a look at the example below: <form [formGroup]='formGroupName' > <input type='text&a ...

AngularJS experiencing issues with Bootstrap multiselect functionality

I am currently integrating bootstrap-multiselect into my AngularJS application. To do this, I've included the following scripts in my master page (index.html). <script src="/bower_components/jquery/dist/jquery.min.js"></script> <scrip ...

In Vue.js, the minus button will automatically disappear once the value reaches 0, and the plus button will reappear

I am trying to implement a functionality where, when the minus button is clicked and the value reaches 0, the minus button gets hidden and the plus button becomes visible again to add values. I have managed to make the minus and plus buttons work but nee ...

Preserving tinyMCE content prior to sending to PHP server

I am facing a challenge with this task. It functions, however, I have to submit the form twice for the tinyMCE value to be stored. $("form#data").on('submit',function(e){ e.preventDefault(); tinymce.init({ selector: " ...

Is there a way to dynamically change the title of a tab when new content is added to a minimized page?

Is anyone familiar with how websites like Facebook and Buzzfeed update their tab titles when there are multiple tabs open? I have noticed that sometimes they add a "(1)" or "(2)" to indicate changes on the page. Do they use JavaScript to achieve this eff ...

"Finding a way to redirect users after clicking on a download link when onclick event is already in use

Below is a snippet of code that I am working with: <input type="checkbox" id="myCheck"> span class="checkmarkz"></span> <a href="test.zip" class="download" onclick="return ifchecked( ...

What is the best way to retrieve state from a selector after refreshing the page?

In my React JS boilerplate code, I have a container component that retrieves data (such as a list of schools) using sagas into a reducer. The state set by the reducer is then read by a selector function on the render page to display to the user. Sample ...

Issues with background image slideshow functionality

For one of my websites, I am using a theme that originally came with a single background image. I modified it to have multiple background images for the 'section' tag <section id="intro" class="intro"></section> U ...

Contrasting Viewport on Android in Landscape and Portrait Modes

I've been working on a mobile site and in order to test it, I've set up different emulators for Android and used an iPhone emulator as well. The issue I am facing is that when I call window.innerWidth upon orientation change on the iPhone, the vi ...

Refreshing a JQuery select control to reload and re-render the options

I have implemented the following functions to enable searching within a select dropdown using an input text field: The first function triggers the search when a key is pressed in the input field: $( 'input#id_nit_del_cliente' ).keyup(function(e ...