如何在vuejs + electron中的“导出默认”范围之外调用方法?

时间:2019-10-27 21:17:43

标签: javascript vue.js electron-builder

当我通过拖动标题栏最大化窗口时,电子会发出“最大化”事件。我想调用存在于“导出默认值”范围内的“最大化”方法。我怎么打电话?

<template>
    <div class="window-titlebar">
        <div class="title">{{ title }}</div>
    </div>
</template>

<script>
const electron = require('electron');
import { ipcRenderer } from 'electron';

export default {
    name: 'window-titlebar',
    data() {
        return {
           title: false
        }
    },
    methods: {
        maximize: function() {
            this.title = true;
        }
    }

}

ipcRenderer.on('maximize', () => {
    // I want to call 'maximize' method here
})

</script>

2 个答案:

答案 0 :(得分:1)

在父级中,定义参考

<WindowTitlebar ref="titlebar" />

然后通过$refs

调用该方法
this.$refs.titlebar.maximize();

答案 1 :(得分:1)

最好在组件本身上注册事件处理程序,那样,您就可以将事件处理程序的存在与组件的生存期相关联,并且事件处理程序将在组件实例的上下文中执行。

export default {
    name: 'window-titlebar',
    data() {
        return {
            title: false
        }
    },
    created() {
        ipcRenderer.on('maximize', this.onMaximize)
    },
    destroyed() {
        ipcRenderer.off('maximize', this.onMaximize)
    },
    methods: {
        onMaximize() {
            this.title = true
        }
    }
}