The main element has not been specified in pagination

Utilizing the Element UI el-pagination component in my project like so:

<el-pagination
        @size-change="handleChange"
        @current-change="CurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[50, 100, 150, 300]"
        :page-size="pageSize"
        popper-class="popper"
        layout="sizes, prev, pager, next"
        :total="getTotal"
      />

I have defined the necessary methods:

CurrentChange(val) {
      this.currentPage = val;
    },

In order to streamline usage across multiple instances, I created a mixin named pagination.js.

To enhance accessibility, I included keyboard event listeners for elements like so:

document.querySelectorAll('ul.el-pager li.number').forEach((element, index) => {
       
        element.addEventListener('keyup', function (e) {
          if (e.key == 'Enter' || e.key == 'Space') {
            this.$root.CurrentChange(element.innerHTML);
          }
        });
      })

However, I am encountering an issue where 'undefined' is returned.

Upon further investigation with console.log(this.$root), it appears that the Vue root context is not accessible as expected. What could be causing this discrepancy?

Answer №1

When the keyup event is triggered, 'this' will refer to the current element instead of the component instance. To access 'this' value as the component instance, you should store it in a variable outside of the forEach callback function.

const self = this;

document.querySelectorAll('ul.pagination li.page').forEach((element, index) => {
   
    element.addEventListener('keyup', function (e) {
      if (e.key == 'Enter' || e.key == 'Space') {
        self.$root.ChangePage(element.innerHTML);
      }
    });
  })

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

Issue with Element UI default theme utilizing an incorrect font path within the NPM package

I am currently facing an issue with my Laravel Mix setup while using Element UI. The problem lies in the fonts path, which is returning a relative URL instead of starting at the root. My setup includes Element UI v2.9.1 and Vue 2.6.10. After trying to man ...

Error message: 'Vue is not defined' when using RequireJS

I'm facing an issue where the Vue object appears to be undefined in the browser after loading Vue with RequireJS. I'm puzzled by this situation and would appreciate any guidance that could help me narrow down the problem. It's worth mention ...

Error: The function __webpack_require__(...) is not defined as a context

Whenever I attempt to dynamically import XML files using the code snippet below: const path = require.context(`../report/${runId}/`, false, /\.xml$/); An error is thrown: Uncaught (in promise) TypeError: __webpack_require__(...).context is not a func ...

Am I utilizing the appropriate method to change the color of my element to red?

I'm currently attempting to change the color of all three headers to red when the "Red Headings" button is clicked by utilizing the changeStuff function. However, it doesn't appear to be functioning correctly. Can someone confirm if this is the a ...

Unable to populate an array with a JSON object using Angular framework

Here is the JSON object I have: Object { JP: "JAPAN", PAK: "PAKISTAN", IND: "INDIA", AUS: "AUSTRALIA" } This JSON data was retrieved as a response from an HTTP GET request using HttpClient in Angular. Now, I want to populate this data into the following ...

Generating documents in Word or PDF format using PHP and Angular data

My goal is to generate a Word document using PHP for which I found a solution involving the use of headers. header("Content-type: application/vnd.ms-word"); header("Content-Disposition: attachment;Filename=output.doc"); Initially, this method worked well ...

How can I use AngularJS to show a JSON value in an HTML input without any modifications?

$scope.categories = [ { "advertiser_id": "2", "tier_id": 1, "tier_name": "1", "base_cpm_price": "", "retarget_cpm": "", "gender": "", "location": "", "ageblock1": "", "ageblock2": "", "ageblock3": ...

How can I insert a variable into the URL for fetching data in React?

Here's a fetch request I have: fetch(`https://example/e1/e11/${variable1}?param=A1&include=metadata`, { "method": "GET"... Is there an issue with declaring the variable1 in this manner? It seems to work fine when written lik ...

What is the best way to trigger an action in response to changes in state within Vuex

Are there more sophisticated methods to trigger actions in vuex when a state changes, rather than using a watcher on that state? I want to ensure the most efficient approach is being utilized. ...

Remove a picture from Cloudinary by utilizing the axios delete method

I am struggling to find a solution for deleting images on Cloudinary using axios delete request. It seems there is no specific API URL or code snippet available that can help me achieve this. Here is the code I am using to upload an image on Cloudinary: ...

Uploading an image along with additional information to Strapi using React

Can you assist me with allowing users to post data on Strapi, such as their name, URL, description, and image? I attempted to add an input type file but encountered a 500 error. I suspect this could be due to the need to call localhost:1337/upload, but I& ...

Requiring a landing page to be shown to each individual visitor

I am in the process of creating a landing page for an upcoming music release. I want to display this splash page every time a visitor lands on the home page, without setting the splash page as the base href. Would using JavaScript be the most effective m ...

Angular2 Error: Issue with the "match" function in JavaScript

Why am I receiving a typeerror that says "cannot read property of 'match' undefined"? var numInput = document.getElementById('input'); // Listen for input event on numInput. numInput.addEventListener('input', function(){ ...

Storybook encounters an undefined error with the Vuex store

Here is a snippet from my main.js file: import './plugins'; import store from './store'; import FileUpload from './components/FileUpload'; export default { install(Vue) { Vue.component('file-upload', ...

Azure App Service encounters difficulty initiating Vue.js

After setting up a pipeline in Azure Dev Ops to build, copy, archive, and deploy a Vue.js app to an Azure App Service on Linux, I encountered an issue. Despite seeing the build outputs in the wwwroot folder when SSH into the server, the URL of the App Serv ...

What is the best way to pass the double-clicked row as a parameter to a function, when the row is dynamically created in the code?

I am facing an issue with adding rows to an empty table using a button and attaching a dblclick event listener to each row. The challenge arises when I need to execute a function that requires the specific row being double-clicked. In tables already popu ...

Vite and Vue3 Error: An unexpected ">" was encountered instead of "setup" while using the "ts" language attribute

While executing the command vite build, the build process fails with the error message Expected ">" but found "setup". Surprisingly, this issue even occurs when using the default HelloWorld.vue component (which is included in the Vite + Vue TS sample pr ...

Exploring the capabilities of the Django FormWizard: utilizing JavaScript for dynamic display of form fields

I'm currently working with Formwizard and I have a step where users need to enter 4 phone numbers. I want to provide an 'Add another phone number' button that they can click to reveal more input fields, controlled by JavaScript. I've se ...

Tips for toggling the display of multiple ion-input fields based on the selected value from an ion-select dropdown

I am working with an ion-select element that contains options from 1 to 10. <ion-label> Select how many input fields</ion-label> <ion-select> <ion-option value="0"> Zero</ion-option> <ion-option value="1"> One</ion- ...

Utilizing AJAX to create objects in Rails

Within a Rails application, there exists a User model with a one-to-many relationship to Words. The scenario involves a table with multiple items (accessible through the show action in the items controller), where each item is presented as a row containing ...