欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > vue >内容正文

vue

通过cordova将vue项目打包

发布时间:2025/4/16 vue 95 豆豆
生活随笔 收集整理的这篇文章主要介绍了 通过cordova将vue项目打包 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

准备工作:需要之前配置好vue-cli脚架构,安装好cordova环境。下面开始对vue.js项目进行打包,打包环境为Android。

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

  • 将myApp1/www/index.html中所有的<meta>拷贝到vue/index.html中
  • 将将myApp1/www/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项目

      终端中进入到myApp1 项目,执行以下命令:  

        cordova platform add android    这时候vue项目中会得到一个android文件夹,里面包含一个测试版本的apk,可以传输到手机上调试。

     

    4.添加cordova插件

      终端中进入到vue项目,执行以下命令:

        cordova plugin add xxxx

    5. 构建 vue项目

      许多人掉过这个坑,打包出来的apk是一个cordova默认的空白项目,因此,需要在打包vue之前在这里把配置文件修改过来。 

      终端中进入到vue项目,执行以下命令:

          npm run build

    6.文件转移

      将dist文件夹下的文件,拷贝到cordova/platforms/androd/assets/www目录下     (index.html替代掉原本的)

     

    7.构建cordova项目

      从终端进入到myApp1/platforms/android/cordova目录下,执行以下命令:

        cordova build android    //构建apk  

     

      配置JDK环境(这里只对mac os进行记录,Win系统请自行脑补):

        cd ~ 进入到 目录

        touch .bash_profile  

        vi .bash_profile         使用vi编辑器编辑 .bash_profile文件

        然后输入   i   ,在vi编辑器里面输入 i  的意思是开始编辑。

        vi编辑器里面的内容如下: 

        JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home

        CLASSPAHT=.:JAVAHOME/lib/dt.jar:JAVAHOME/lib/dt.jar:JAVA_HOME/lib/tools.jar

        PATH=JAVAHOME/bin:JAVAHOME/bin:PATH:

        export JAVA_HOME

        export CLASSPATH

        export PATH   

        然后退出vi编辑器使用如下命令:(个人习惯  :wq!回车 )

        1. 输入 ese 

        2. 输入冒号 : wq

        3. 保存退出 

      如果以上修改完毕切正确,那么接下来就是让配置的环境变量生效,使用如下命令:

        source .bash_profile  

      完毕以后查看下当前的java 版本是否正确输入如下命令:

        java -version

       如果是预想中的1.8,那么恭喜你,你这个时候就可以执行:  cordova build android

     

        cordova run android       //构建apk,并安装到连接的设备上  (按个人需求)

     

    转载于:https://www.cnblogs.com/hanli-you/p/8533737.html

    总结

    以上是生活随笔为你收集整理的通过cordova将vue项目打包的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。