javascript - Vue 无法监听 Co​​rdova 事件

标签 javascript cordova vue.js

我正在尝试使用 Cordova 构建一个混合应用程序。我正在使用 VueJS 进行路由和 AJAX 请求。

不幸的是,我无法捕获某些 Cordova 事件。甚至 deviceReady 事件都不起作用。
这是我的文件:

require('./bootstrap');


var Vue = require('vue');
var VueRouter = require('vue-router');

Vue.use(VueRouter);

// Some components
Vue.component('test', require('./Vue/components/test.vue'));
Vue.component('mainnav', require('./Vue/partials/mainnav.vue'));

// Route-components
const Home = Vue.component('home', require('./Vue/pages/home.vue'));
const Login = Vue.component('login', require('./Vue/pages/auth/login.vue'));
const Register = Vue.component('register', require('./Vue/pages/auth/register.vue'));
const notFound = Vue.component('notFound', require('./Vue/pages/404.vue'));

// the routes
const routes = [
    { path: '', component: Home },
    { path: '/', component: Home },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '*', component: notFound }
];

const router = new VueRouter({
    mode: 'history',
    routes // short for routes: routes
});

const vueApp = new Vue({
    router,
    mounted: function(){
        //alert('VueJS is ready!');
        document.addEventListener('deviceReady', this.onDeviceReady, false);
    },
    methods: {
        onDeviceReady: function() {
            alert('Device is ready!');
        }
    }
}).$mount('#app');

也许我没有收到消息,因为设备在 Vue 准备就绪之前就已经准备好了。但我该如何处理呢?

我可以访问其他选项,例如来自 Vue root-instance 和来自 vue 组件的 vibration-plugin:

export default {
    data() {
        return {
            vibrateDuration: 5000,
        };
    },
    methods: {
        letsVibrate: function(){
            navigator.vibrate(this.vibrateDuration);
        }
    }
}

知道如何在 Vue 中捕获设备就绪事件吗?

最佳答案

可能是并发的问题。尝试设置一些简单的信号量锁,仅当两者都打开时才触发一个函数(未测试,但你明白了):

let deviceReady = false
let vueMounted = false

const vueApp = new Vue({
  router,
  mounted: function(){
    vueMounted = true
    if (deviceReady) vueApp.everythingReady()
  },
  methods: {
    everythingReady: function() {
        alert('Vue is mounted and everything is ready')
    }
  }
}).$mount('#app')

document.addEventListener('deviceReady', () => {
  deviceReady = true
  if (vueMounted) vueApp.everythingReady()
}, false)

关于javascript - Vue 无法监听 Co​​rdova 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40984761/

相关文章:

javascript - Vue.js 2.5.16 中数字/字符串的 Prop 行为不一致

Javascript 表单验证未执行且输入文本框高度光标定位未设置到顶部。

ios - 带有 Apple Watch 的 Cordova 应用程序无法在 Apple Store 上载(有多个 bundle 与 CFBundleIdentifier 冲突)

Vue.js 简单的 Accordion 如何打开和关闭同一个项目

android - phonegap 构建失败 : Execution failed for task ':processDebugResources'

android - 在 <div> 中显示 AR 浏览器。是否可以?

javascript - 避免直接修改 prop,因为只要父组件重新渲染,该值就会被覆盖

javascript - 将 performance.mark() 与 Chrome 开发工具性能选项卡一起使用

javascript - 创建动态 JavaScript 变量,其中左侧和右侧都是动态的

javascript - 无法播放通过 JS 创建的嵌入在 iframe 中的 youtube 视频