需要在展示页里调用顶部导航栏页里的方法,两者之间没有引用关系,看了一下vue的API发现可以用这个方法实现。
https://cn.vuejs.org/v2/api/#vm-on
可以看到需要同一个vue实例来调用两个方法。所以可以建立一个中转站。
首先在任意位置新建util.js文件。
-
import Vue
from
'vue'
-
export
default
new Vue
然后在两个页面都引入它,注意引入路径。
import Utils from '../utils.js';
然后是调用方:
-
methods: {
-
functionA() {
-
Utils.$emit(
'demo',
'msg');
-
}
-
}
最后是被调用方:
-
mounted(){
-
var that =
this;
-
Utils.$on(
'demo',
function (msg) {
-
console.log(msg);
-
that.functionB();
-
})
-
},
-
methods: {
-
functionB() {
-
...
-
}
-
}