假設A.vue與B.vue頁,A.vue需要調用B.vue頁面methods中的test()方法
B.vue
methods: {
test(){
console.log('hello word!')
},
}
方法一:組建公共的js,在src下新建utils目錄,新建callUtils.js
callUtils.js:只需寫這兩句即可
import Vue from 'vue'
export default new Vue
在A.vue中的點擊事件中寫一句
click(){
Utils.$emit('test','B')
}
在B.vue中寫
methods :{
test(){
console.log("hello word!")
}
},
mounted() {
Utils.$on('test',(B)=>{
this.test();
})
}
方法二:利用window
A.vue在點擊事件寫入
click(){
window.parent.test(event, 'A');
}
B.vue的mount()寫
methods: {
test(){
console.log('hello word!')
}
}
mounted () {
var that = this;
window['test'] = (event, name) => {
that.test(event, name)
}
}