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 id="deviceready" class="blink">
  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,並安裝到連接的設備上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM