Vue—how to call a method on another page from one page

1. Establish a transfer station

Create a util.js transfer station file (anywhere, I am in /assets/js/util.js)

import Vue from 'vue'
export default new Vue

2. Introduce the file on two pages respectively (note the path)

例如:import events from '@/assets/js/util.js'

3. Caller code

methods: {
    functionA() {
        Utils.$emit('demo','msg');
    }
}

Fourth, the callee code

mounted(){
    var that = this;
    Utils.$on('demo', function (msg) {
        console.log(msg);
        that.functionB();
    })
},
methods: {
    functionB() {
        ...
    }
}

Guess you like

Origin blog.csdn.net/zlfjavahome/article/details/131570746