1.添加cordova项目
$ cordova create myApp1 org.apache.cordova.myApp myApp2
其中:
- myApp1:cordova目录名
- org.apache.cordova.myApp: 包名
- myApp2: 项目名(在config.xml的<name>中查看)
2.在vue中添加cordova的配置
myApp1/www/index.html----->vue/index.html
<html>
<head>
- <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</div>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
- <meta>拷贝到vue/index.html中
- <script>关于cordova.js的引用拷贝到vue/index.html中
myApp1/www/js/index.js----->vue/vuex/main.js
var app = {
// Application Constructor
initialize: function() {
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
1)内容拷贝到vue/src/vuex/main.js中
2)onDeviceReady时启动app
onDeviceReady: function () {
//app.receivedEvent('deviceready');
appRouter.start(App, 'app')
window.navigator.splashscreen.hide()
}
3.创建android项目
cordova platform add android
4.添加cordova插件
cordova plugin add xxxx
5. 构建 vue项目
npm run build
6.文件转移
将dist文件夹下的文件,拷贝到cordova/platforms/androd/assets/www目录下
7.构建cordova项目
cordova目录下:
cordova build android //构建apk
cordova run android //构建apk,并安装到连接的设备上