Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import JS script in vue loader component?

I have a vue-component

vue-compoennt (vue-loader)

<template>
  <p>This is a template</p>
</template>
<script>
  require('main.js')
  huha()
</script>

And I have

main.js

 var huha = function(){
      alert("this is huha");
    }; 
alert("this is simple alert");

Here I get the 'simple alert' but in assessing huha() it is showing reference error. Can someone please help me to understand why is this happening?

Edit

I am trying to use testimonial.js as following and I am getting reference error.

    <template>
      <p>This is a template</p>
      <div id="testimonial-slider"></div>
    </template>
    <script>
      require('testimonial/testimonial.js')
      require('testimonial/testimonial.css')
      var testimonial = new Testimonial('#testimonial-slider');
    </script>
    <style>
      p{
         color: red;
        }
    </style>

It is giving "reference error: Testimonial is not defined"

like image 385
Ashvini Kumar Avatar asked Dec 09 '15 15:12

Ashvini Kumar


1 Answers

You need to export a function like so:

module.exports = {
    huha: function(){
      return alert("this is huha");
    }
}; 

And then in you components file:

<template>
  <p>This is a template</p>
</template>
<script>
  var main = require('main.js')
  main.huha()
</script>
like image 165
Helder Lucas Avatar answered Sep 24 '22 15:09

Helder Lucas