How to incorporate a JavaScript class into a VueJS project

Looking to incorporate a JavaScript class into my Vue application.

The class structure is as follows:

class className { 
   constructor() { 
       ... 
   }  

   function1() { 
       ... 
   }  

   static funtion2() {
        ... 
   } 
}

Attempting to import this class into my application using:

  • import className from './fileName.js';
  • var {className} = require('./fileName.js')

However, in all cases when attempting to call a function of the class (className.function2()), the function is coming up as undefined.

Answer №1

In order to use the import/require functionality, you must export the class first

//1. To use import syntax
export default class className {...}

//2. To use require syntax
class className {}
module.exports.className = className
//or
module.exports = {
    className: className
}

Answer №2

When employing the import/export functionality, the syntax would be as follows:

export class className {}

and you would also include:

import {className} from '<file>';

Answer №3

In contrast to @baao's response, I have included the use of the default keyword for the export.

export default class className {}

Additionally, in the file component.vue:

<script>
import className from '<pathToFile>';
...
</script>

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

What can cause an "Undefined index" error in an ajax POST request and/or PHP script?

When attempting to send an ajax POST request to a PHP file, I encounter an issue where the PHP file returns a notice of "undefined index" and does not receive the value being sent. Despite researching extensively to find a solution, I have been unable to r ...

Solving the Issue of Handling Custom Events in Javascript

I've been experimenting with a simple CodePen that features a basic table with three rows. I'm trying to attach event handlers to each row in the table and trigger the event by pressing a button. However, I'm facing an issue where the attac ...

AJV is failing to validate my body using the function generated by the compile method

Currently, in my API development process with express, I have implemented AJV as a middleware to validate the incoming body data. The version of AJV being used is 6.12.6 Below is the JSON schema named body-foobar.json: { "type": "object& ...

Troubleshooting: Issue with append function not functioning properly after click event in Angular

I am struggling to implement a basic tooltip in AngularJS. Below is the HTML I have: <span class="afterme" ng-mouseover="showToolTip('this is something', $event)" ng-mouseleave="hideToolTip();"> <i class="glyphicon glyphicon-exclama ...

How come my total isn't zero after I get a 1 on the dice roll?

I've been working on a dice game where if either of the dice rolls a 1, the score is set to 1. However, I'm having trouble getting that part of my code to work properly. I'm confident that everything else is functioning correctly. v ...

Access information from JSON file

Looking to extract data from an external JSON file and store it in a JavaScript array for manipulation? Here is a snippet of the JSON file for reference: "Country":[ { "Country_Name":"India", "Country_Details":[ { ...

Generate HTML output from the container element of a Vue application

On my HTML page, I have integrated a Vue application that consists of a single component. This application is attached to an element with the id paid-content. index.html <div id="paid-content"> <h2>You should see this if you' ...

Error encountered on Windows 10 version 10.0.19044 during library installation with npm

I've encountered an error while trying to run or install libraries for a project within our organization. E:\GIT\bookshelf>npm install npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail= ...

Troubles encountered while deploying Vue.js application using IBM Cloud and Cloud Foundry Node Buildpack

I am completely new to using IBM Cloud for the first time and need some assistance. I am looking to deploy my Vue.js app on IBM Cloud with continuous delivery. The Vue project is stored in a GitHub repository, and I want it to be deployed automatically whe ...

Evaluate the functionality of an Angular controller method that interacts with the Document Object Model (

We currently have an AngularJS controller that contains the following function: $scope.testMe = function() { return $('#test'); } So, how can we effectively test this function? We attempted a Karma test as shown below: describe(' ...

Moving Configuration Files in NextJS

When working on a typical Next.js project, I often end up with several root-level configuration files: tsconfig.json next.config.js next-seo-config.ts .eslintrc etc... I am looking to tidy up my root directory by moving these files into their own separat ...

Send a form without the need to reload the page

I have a form that inserts values into the database successfully. However, each time the values are submitted, the page refreshes. I attempted to find a solution on this website, but it didn't work for me. Can someone please advise me on how to submit ...

Is there a native feature in Vue.js that allows for the addition of a duplicate of a constantly saved object to an array that is repeated

Having an issue with my Vue.js app where adding a newItem to a list of items results in the added object being bound to the input. Here's what I've tried so far: new Vue({ el: '#demo', data: { items: [ { start: & ...

JavaScript function encountered an error due to an expected object

Currently, I am in the process of developing an application using VS2015, JavaScript, Angular, and MVC 5. Here is an excerpt of my JavaScript code: var myApp = angular.module('QuizApp', []); myApp.controller('QuizController', [&apos ...

When JSON data is copied and displayed in Node.js or JavaScript, it appears as a string

I have written a code to copy one JSON file into another JSON file. Here is the code: function userData(){ fs.copyFile( path.join(process.cwd(), "user/access.json"), path.join(process.cwd(), "client/access.json"), ...

A mysterious issue arose while trying to retrieve the script (Service Worker)

After disconnecting from the internet, my service worker is generating the following error: (unknown) #3016 An unknown error occurred when fetching the script This is what my service worker code looks like: var version = 'v1' this.addEventLis ...

Converting an array of object keys into integers only can be done using JavaScript

Here is an array of objects with various nutrients and their values: [ {nutrient: "Energy", per100: "449kcal", per35: "157kcal", id: 6} {nutrient: "Fat", per100: "24.4g", per35: "8.6g", id: 1} {nutrient: "Saturated fat", per100: "4.5g", per35: "1.6g ...

How do I go about showing every character on a separate line using a for loop?

var input = prompt("What is Lance attempting to convey?"); //user enters any text for (var i = 0; i <= input.length; i++) { var output = input.charAt(i); if (output == "e" || output == "o" || output == "a" || output == "u") { outp ...

My changes to the HTML file are not being reflected in the browser, even after clearing the cache. This is happening in an Angular and Node.js application

I'm currently developing an application using Angular and Node.js. I've noticed that when I make changes to the HTML file, the browser isn't updating even after clearing the cache. Could this be a coding issue? Any suggestions on how to fix ...

What is causing the element to disappear in this basic Angular Material Sidenav component when using css border-radius? Check out the demo to see the issue in action

I have a question regarding the Angular Material Sidenav component. I noticed that in the code below, when I increase the border-radius property to a certain value, the element seems to disappear. <mat-drawer-container class="example-container" ...