我正在尝试使用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 vibration-plugin
的{{1}}和vue组件:
root-instance
任何想法,我如何在Vue中捕获设备就绪事件?
答案 0 :(得分:3)
这可能是一个并发问题。尝试设置一些简单的信号量锁,只有当两者都打开时才触发一个函数(未经测试,但你明白了):
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)
答案 1 :(得分:0)
尝试:
vueApp = new Vue({
//...
methods: {
onDeviceReady: function() {
alert('Device is ready!');
}
}
});
document.addEventListener(
'deviceready',
vueApp.onDeviceReady
);
答案 2 :(得分:0)
对于vue应用,您必须将<script src="cordova.js"></script>
明确添加到public/index.html
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title>My app</title>
</head>
<body>
<script src="cordova.js"></script> <!-- dude, add this -->
<div id="app"></div>
<noscript>
<strong
>We're sorry but ia doesn't work properly without JavaScript enabled.
Please enable it to continue.</strong
>
</noscript>
</body>
</html>