6

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"

4
  • Can I see your component code? Commented Dec 9, 2015 at 17:23
  • Actually i am using vue-loader webpack in this app. So in vue-component we write html, script and style in same file with extension '. vue-component' so i haven't defined a separate component. My component is 'vue-component' Commented Dec 9, 2015 at 17:44
  • module.exports only works with a compilar, like browserify. Whats your build process? Commented Dec 10, 2015 at 4:58
  • 1
    I am using webpack. I am also using vue-loader Commented Dec 10, 2015 at 5:03

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I have done this but it's not working? I require 'testimonial.js' library so when I do 'require("testimonial")' then use this libraries functions and objects then reference error is thrown.
@AshviniKumar How exactly do you use the library? Every component is scoped. You're probably referencing the library out of scope.
@YauheniPrakopchyk I have updated the question, Please have a look at the edited version of the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.