Create a prototype class in NuxtJS Vue

What is the correct way to set a class to prototype in Vue NuxtJS?

I have created a plugin

Here is my nuxt.config.js file:

plugins: [
    { src: "~/plugins/global.js" },
],

The global.js file contains:

import Vue from "vue";
import CustomStore from "devextreme/data/custom_store";

// Trying to set it in the prototype
Vue.use(CustomStore)

However, I encounter an error that says:

A class must be instantiated using the 'new'

Although I know this approach is incorrect, I cannot find any resources on how to properly initialize it. One attempt was:

Vue.use(new CustomStore());

This removes the error, but how do I call it?

My aim is to use something like this in my component:

this.dataSource = this.$CustomStore({ ///... settings...// })

Answer №1

It seems like CustomStore is a function, so you may want to consider utilizing the Nuxt.js inject() method. By using this method, you can make your functions or values accessible throughout your application.

~/plugins/global.js

export default ({ app }, inject) => {
  // Inject $CustomStore() in Vue, context and store.
  inject('CustomStore', CustomStore)
}

Afterwards, you will be able to use it in various components of your application.

Your Component

 mounted() {
   this.$CustomStore()
 },

For more information, you can refer to https://nuxtjs.org/docs/2.x/directory-structure/plugins#inject-in-root--context

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

Global registration of single file components is necessary to utilize them across

I need some help with VueJS. I am trying to create a registration form, but I am encountering an error when registering the component globally. This is all new to me, so any assistance would be greatly appreciated. Here is the specific error message that ...

After refreshing the page, the ngRoute white page is displayed

I've encountered an issue with my Angular website. I built it using ngRoute, but when I click on a link, a white page appears. Only after refreshing the page does the content actually show up. Even in the browser's DevTools, you can see the html ...

Enhance your VueJS and Tailwind development with dynamic classes and variables

I'm attempting to incorporate dynamic variables (props) into a Tailwind CSS class, but I seem to be encountering an issue: :class="`w-${percent}/12: ${show}`" Upon running this code, the output is as follows: <div class="w-0 h-2 tra ...

Countdown component in Ant Design failing to display correct date

I’m currently working on developing a specific date component using react in conjunction with antd. Below is the code snippet I am utilizing: import { Statistic, Col, Row } from 'antd'; const { Countdown } = Statistic; const deadline = Date.pa ...

Tips for implementing a document ready function on a nested page within a larger full-page website

I am currently working on a website that utilizes fullpage.js, but the same principle applies to all single-page websites. I am trying to figure out how to implement the $(document).ready() function on a 'nested' page within the site. Since every ...

Adding multiple variables in jQuery: A guide to mimicking the .= operator from PHP

Being new to jQuery, I'm a bit unsure of how to achieve this. Typically in php, I can accomplish something like this: $result = ''; $result .= 'Hi'; $result .= ' there'; echo $result; I'm simply wondering if there ...

Using a loop to execute Javascript Promise.all()

I am currently facing an issue where I need to make a web API call twice inside a loop, and then wait for the results before pushing them into a larger array as subarrays. The code snippet below illustrates my approach: var latlngPairs = []; function extra ...

Drawing intersecting lines on a javascript canvas and clearing them using the lineto method

I am working with a canvas where lines are drawn based on mouse movement. I am trying to achieve the effect of the line only lasting for a few seconds before disappearing, similar to swirling a ribbon with a set length. I have been using lineTo to draw the ...

Navigating to the default landing page using basic authentication middleware in Express

I'm currently working on implementing basic authorization for an entire website using Express. The goal is to have users enter their credentials, and if correct, they will be directed to the standard landing page. If the credentials are incorrect, the ...

Encountered a problem when incorporating delay functions into redux-saga testing using the Redux Saga Test Plan library

I am facing a challenge while trying to test my redux-saga functions using the Redux Saga Test Plan library. The issue arises due to delay functions present in my saga. All tests pass smoothly without any errors when I remove the line containing yield del ...

How is it that my initial response appears correct in the first log, but then suddenly changes to a 1?

I've encountered a strange issue where the response appears correctly in the initial log, but later changes to a '1' when console.log(data); is called. The screenshot illustrates the pattern: https://i.sstatic.net/zaXcg.png If you expand ...

Navigating to a specific section within a page containing multiple sections in Vuejs: How to access the route and unique identifier

Within my navigation bar, I have a link that looks like this: <li class="nav-item"> <router-link to="/branding" class="nav-link">Branding</router-link> </li> The link is functioning properly and ...

Tips for implementing camera animation to focus on a specific area of a gltf model when it is clicked in three.js

I've successfully used a gltf loader to load a model and implemented a click function on it. The desired behavior is that when a specific part of the model is clicked, the camera should smoothly transition to focus on that part instead of abruptly cha ...

Having trouble with the npm package installation for react-circular-progressbar

I encountered an error when trying to install react-circular-progressbar in my React projects. Why is this happening? npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree npm ERR! npm ERR! While resolving: <a href="/cdn-cgi/l/em ...

AWS Lambda applies quotes to create the event input

I'm encountering a problem with my Python 3.8 AWS Lambda function that is meant to handle form inputs from a web application. The data from the form inputs gets passed to the Lambda function and ends up in the event dictionary. However, lambda seems t ...

What are the steps to generate an npm package along with definition files?

Is it possible to create an NPM package with definition files containing only interfaces declared in *.ts files? Consider a scenario where we have two interfaces and one class definition: export interface A { id: number; } export interface B { name: s ...

"Implementing a dynamic way to assign values to different item types in React

There is an object with multiple values inside: const [sort, setSort] = useState({ "city": [], "price": [], "year": [] }); When the "add" button is clicked, the "city" value should be updated to include certain va ...

Implementing Vue.js functionality to dynamically add or remove values from an array based on the state of a checkbox

I recently embarked on my journey to learn vue.js and I've encountered a challenging issue. I have dynamic data that I render using a 'v-for' loop. Additionally, I have an empty array where I need to store checked checkbox data and remove it ...

Pass data to PHP using AJAX

Is it possible to pass the variable rowNumber to the PHP file dataSource in this code? function getData(dataSource, divID,rowNumber) { if(XMLHttpRequestObject) { var obj = document.getElementById(divID); XMLHttpRequestObject.open("GET", dataSo ...

Having trouble with JQuery blur and enter key functionality not working properly

I'm currently utilizing the code below for inline editing. I am looking to trigger ajax when either the enter key is pressed or the user clicks away (blur). While everything works fine when the enter key is pressed, the AJAX call does not fire on bl ...