好耶!通过cordova用Vue写安卓APP
基础环境
- 确保你有node.js环境
- 有Java JDK + Android SDK + AVD(安卓虚拟机)
安装Vue与Cordova
1 | npm i -g vue-cli # 安装vue |
快速开始
创建cordova项目
1 | cordova create demo com.example.demo DEMO |
名称 | 必填 | 描述 |
---|---|---|
demo | 是 | 工程的文件夹名称 |
com.example.demo | 否 | 应用程序的id,不填默认为io.cordova.hellocordova |
DEMO | 否 | APP 名称 |
在cordova项目中添加Vue项目
1 | cd demo |
项目结构:
1
2
3
4
5
6
7
8
9
10
11drwxr-xr-x - lianhaocheng 26 4 21:34 .
.rwxr-xr-x 50 lianhaocheng 26 4 21:34 ├── build.sh
.rw-r--r-- 934 lianhaocheng 26 4 20:48 ├── config.xml
.rw-r--r-- 154 lianhaocheng 26 4 21:33 ├── Makefile
drwxr-xr-x - lianhaocheng 26 4 20:48 ├── node_modules
.rw-r--r-- 28k lianhaocheng 26 4 20:48 ├── package-lock.json
.rw-r--r-- 619 lianhaocheng 26 4 20:48 ├── package.json
drwxr-xr-x - lianhaocheng 26 4 20:48 ├── platforms
drwxr-xr-x - lianhaocheng 26 4 20:48 ├── plugins
drwxr-xr-x - lianhaocheng 26 4 20:57 ├── vue-app
drwxr-xr-x - lianhaocheng 26 4 21:34 └── www
在
vue-app/index.html
的head
中添加下面代码1
2
3
4<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 *; img-src 'self' data: content:;">
<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">修改
vue-app/config/index.js
中的build
1
2
3
4
5
6
7
8
9
10build: {
// Template for index.html
index: path.resolve(__dirname, '../../www/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../../www'),
assetsSubDirectory: './static',
assetsPublicPath: './',
...
}在
vue-app/index.html
的body
中添加1
2
3
4
5<body>
<div id="app"></div>
<script type="text/javascript" src="cordova.js"></script>
<!-- built files will be auto injected -->
</body>
打包或运行
打包vue项目
1
npm run build
打包安卓apk
1
cordova build android
不打包直接运行
1
cordova run android
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 RhythmLian's Blog!
评论