vue + cordova

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

  1. <html>
  2. <head>
  3. <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 *">
  4. <meta name="format-detection" content="telephone=no">
  5. <meta name="msapplication-tap-highlight" content="no">
  6. <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
  7. <link rel="stylesheet" type="text/css" href="css/index.css">
  8. <title>Hello World</title>
  9. </head>
  10. <body>
  11. <div class="app">
  12. <h1>Apache Cordova</h1>
  13. <div >
  14. <p class="event listening">Connecting to Device</p>
  15. <p class="event received">Device is Ready</p>
  16. </div>
  17. </div>
  18. <script type="text/javascript" src="cordova.js"></script>
  19. <script type="text/javascript" src="js/index.js"></script>
  20. </body>
  21. </html>
  • <meta>拷贝到vue/index.html中
  • <script>关于cordova.js的引用拷贝到vue/index.html中

myApp1/www/js/index.js----->vue/vuex/main.js

  1. var app = {
  2. // Application Constructor
  3. initialize: function() {
  4. this.bindEvents();
  5. },
  6. // Bind Event Listeners
  7. //
  8. // Bind any events that are required on startup. Common events are:
  9. // 'load', 'deviceready', 'offline', and 'online'.
  10. bindEvents: function() {
  11. document.addEventListener('deviceready', this.onDeviceReady, false);
  12. },
  13. // deviceready Event Handler
  14. //
  15. // The scope of 'this' is the event. In order to call the 'receivedEvent'
  16. // function, we must explicitly call 'app.receivedEvent(...);'
  17. onDeviceReady: function() {
  18. app.receivedEvent('deviceready');
  19. },
  20. // Update DOM on a Received Event
  21. receivedEvent: function(id) {
  22. var parentElement = document.getElementById(id);
  23. var listeningElement = parentElement.querySelector('.listening');
  24. var receivedElement = parentElement.querySelector('.received');
  25. listeningElement.setAttribute('style', 'display:none;');
  26. receivedElement.setAttribute('style', 'display:block;');
  27. console.log('Received Event: ' + id);
  28. }
  29. };
  30. app.initialize();

1)内容拷贝到vue/src/vuex/main.js中

2)onDeviceReady时启动app

  1. onDeviceReady: function () {
  2. //app.receivedEvent('deviceready');
  3. appRouter.start(App, 'app')
  4. window.navigator.splashscreen.hide()
  5. }

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,并安装到连接的设备上