vue項目需要添加的代碼:
- index.html添加代碼塊
<!-- 引入cordova.js -->
<script type="text/javascript" src="cordova.js"></script>
<!-- 掃一掃包的引用 -->
<script type="text/javascript" src="cordova_plugins.js"></script>
- main.js 全局配置如下代碼塊,先加載cordova插件,再執行vue函數:
document.addEventListener('deviceready', function () {
new Vue({
el: '#app-box', //index.html中div的id
router, // 路由對象,沒有則不要
components: { App }, //app.vue
template: '<App/>'
})
window.navigator.splashscreen.hide()
}, false)
- 需要調用掃碼的頁面:
//應用vux的Xbotton按鈕控件,沒用vuxui庫自己寫一個botton按鈕綁定調用掃碼函數即可
<x-button mini type="primary" @click.native="handleSmClick">掃 碼</x-button>
//主要掃碼函數
handleSmClick(){
let that =this
cordova.plugins.barcodeScanner.scan(
function (result) {
/*alert("We got a barcode\n" +
"Result: " + result.text + "\n" +
"Format: " + result.format + "\n" +
"Cancelled: " + result.cancelled)*/
let code =result.text
console.log(code)
},
function (error) {
console.log(error)
},{
preferFrontCamera : false, // iOS and Android 前置攝像頭?
showFlipCameraButton : true, // iOS and Android //前後置攝像頭開關
showTorchButton : true, // iOS and Android //閃光燈開關
torchOn: false, // Android, launch with the torch switched on (if available) //閃光燈
saveHistory: false, // Android, save scan history (default false) //歷史掃碼記錄
prompt : "將條形碼放入掃描區域內", // Android //下方提示文字
resultDisplayDuration: 0, // Android, display scanned text for X ms. 0 suppresses it entirely, default 1500 設置掃碼完成後返回成功對象的時間,默認延時1.5s
formats : "QR_CODE,PDF_417", // default: all but PDF_417 and RSS_EXPANDED
orientation : "", // Android only (portrait|landscape), default unset so it rotates with the device 橫屏豎屏
disableAnimations : true, // iOS
disableSuccessBeep: false // iOS and Android
}
)
}
- 編譯vue項目
npm run build
生成的dist文件夾內的文件 替換掉cordova項目www文件夾下的文件
vue的相關文件
Cordova項目:
- 新創建一個Cordova項目
cordova create d:/hello com.example.hello HelloWorld
cd到hello文件夾下操作
2. 添加對應平臺(本次測試用android)
//添加android平臺的編譯
cordova platform add android
- 添加掃一掃插件
cordova plugin add phonegap-plugin-barcodescanner --save
- 替換vux項目編譯後的文件
- 生成最終安裝包apk文件(debug版)
cordova build android
大功告成 apk安裝到手機測試~~~~~~~~~~~~~~~~~~~~~