兩個問題 首先是怎樣引入的問題:
首先說明: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 } //很重要 要暴露出去