如何使用 Electron + Vue (SPA) 处理事件

How to handle events using Electron + Vue (SPA)

我在弄清楚如何将 Vue 与 Electron 一起使用时处理事件时遇到问题。这可能看起来很愚蠢,但我已经花时间阅读文档,在浏览器中测试 Vue 实例和指令,它们工作正常但相同的原则在我的电子桌面应用程序中不起作用(这与 Php 有很大不同)面向对象)。

我使用 electron-vue 样板文件,进行设置,效果非常好。创建了一个模板和一个组件 (TopMenu),现在我需要处理放置在我的 TopMenu 组件中的菜单按钮的点击事件,但无论我如何尝试,我得到:

[Vue warn]: Property or method "say" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option. (found in component ) [Vue warn]: Handler for event "click" is undefined.

./LandingPageView/TopMenu.vue:

<template>
  <div>
    <button type="button" name="button" v-on:click="say">BUTTON</button>
  </div>
</template>

main.js:

import Vue from 'vue'
import Electron from 'vue-electron'
import Resource from 'vue-resource'
import Router from 'vue-router'

import App from './App'
import routes from './routes'

import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.min.js'

Vue.use(Electron)
Vue.use(Resource)
Vue.use(Router)
Vue.config.debug = true

const router = new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes
})

/* eslint-disable no-new */
new Vue({
  methods: {
    say: function () {
      alert()
    }
  },
  router,
  ...App
}).$mount('#app')

App.vue:

<style>
  @import url(https://fonts.googleapis.com/css?family=Lato:400,700);

  * {
    margin: 0;
    padding: 0;
  }

  html,
  body { height: 100%; }

  body {
    align-items: center;
    background:
      radial-gradient(
        ellipse at center,
        rgba(255, 255, 255, 1) 0%,
        rgba(229, 229, 229, .85) 100%
      );
    background-position: center;
    font-family: Lato, Helvetica, sans-serif;
  }
</style>

<template>
  <div>
    <router-view></router-view>
  </div>
</template>

<script>
  import store from 'src/vuex/store'

  export default {
    store
  }
</script>

index.ejs:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <div id="app"></div>
    <!-- webpack builds are automatically injected -->
  </body>
</html>

您好,您需要将方法放在与模板相同的组件中:

<template>
  <div class="example" @click="say">say method</div>
</template>

<script>
  export default {
    methods: {
      say () {
        console.log('Hello world!')
      }
    }
  }
</script>

查看vue文档:https://vuejs.org/v2/guide/