我有一个 vue 组件
vue-compoennt (vue-loader)
<template>
<p>This is a template</p>
</template>
<script>
require('main.js')
huha()
</script>
我有
main.js
var huha = function(){
alert("this is huha");
};
alert("this is simple alert");
这里我得到了“简单警报”,但在评估 huha() 时它显示了引用错误。有人可以帮我理解为什么会这样吗?
编辑
我正在尝试使用 testimonial.js,但出现引用错误。
<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>
它给出“引用错误:未定义推荐”
最佳答案
你需要像这样导出一个函数:
module.exports = {
huha: function(){
return alert("this is huha");
}
};
然后在你的组件文件中:
<template>
<p>This is a template</p>
</template>
<script>
var main = require('main.js')
main.huha()
</script>
关于javascript - 如何在vue loader组件中导入JS脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34182732/