两个问题 首先是怎样引入的问题:
首先说明:js文件不能放在components文件夹下
方法一: 在组件内直接引用
`import swiper from './swiper.js'`
方法二 : 全局注册引用
function aa(){
console.log("11");
}
export { one }
import one from './common/js.js';
Vue.prototype.ss=one;
this.ss.aa(); //在需要的地方使用
按照一定顺序加载js的方法:
方法一: 相当于clickOne事件运行时加载js文件
组件内代码:
<script>
import {one} from '../js/one.js' //接收js内暴露的对象
export default {
data () {
return {
testvalue: '11'
}
},
methods:{
clickOne:function(){
one(); //运行
}
}
}
</script>
one.js内代码:
function ss() {
console.log('1111111111');
}
export { one } //很重要 要暴露出去
方法二: 与方法一类似 就是在mounted
钩子函数内调用
<script>
import {one} from '../js/one.js' //接收js内暴露的对象
export default {
data () {
return {
testvalue: ''
}
},
mounted(){
clickOne:function(){
one(); //运行
}
}
}
</script>
one.js内代码:
function ss() {
console.log('1111111111');
}
export { one } //很重要 要暴露出去