Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import javascript class in vueJS

I want to use a javascript class in may Vue application.

My class looks like:

class className { 
   constructor() { 
       ... 
   }  

   function1() { 
       ... 
   }  

   static funtion2() {
        ... 
   } 
}

I tried to import this class in my application like:

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

In all cases I receive when I want to call a function of the class (className.function2()): the function is undefined.

like image 409
Kutas Tomy Avatar asked May 07 '18 13:05

Kutas Tomy


People also ask

Can I use JavaScript in Vue?

A Vue application/web page is built of components that represent encapsulated elements of your interface. Components can be written in HTML, CSS, and JavaScript without dividing them into separate files.

Can you use vanilla JavaScript in Vue?

Vue is a JavaScript framework and therefore you can insert vanilla code anywhere in it and it will run perfectly fine.


1 Answers

You need to export the class to be able to import/require it

//1. For import syntax
export default class className {...}

//2. For require syntax
class className {}
module.exports.className = className
//or
module.exports = {
    className: className
}
like image 154
Tnc Andrei Avatar answered Sep 17 '22 14:09

Tnc Andrei