vue調起微信掃一掃,兩個注意的點
1、url必須是不帶參的地址欄,如果傳了帶參數的地址url有可能會出現安卓機能調,蘋果機報錯或者安卓和蘋果都報錯
2、this指代問題在vx.ready等等方法里面此時的this指代的並不是vue實例,此時就應該在方法開頭賦值一個對象為vue實例了
onScan(){ const _this = this this.$axios .get("你的后台地址", { params: { url: location.href.split("#")[0]//你的不帶參的當前頁url } }) .then(response => { let res = response.data; wx.config({ debug: false, appId: res.data.jsSdkUiPackage.appId, timestamp: res.data.jsSdkUiPackage.timestamp, nonceStr: res.data.jsSdkUiPackage.nonceStr, signature: res.data.jsSdkUiPackage.signature, jsApiList: ["scanQRCode"] }); }); wx.error(function (res) { Dialog.alert({ title: "提示", message: res.errMsg }).then(() => {}); }); wx.ready(function () { wx.checkJsApi({ jsApiList: ['scanQRCode'], success: function (res) { } }); wx.scanQRCode({ needResult: 1, // 默認為0,掃描結果由微信處理,1則直接返回掃描結果, scanType: ["qrCode"], // 可以指定掃二維碼還是一維碼,默認二者都有 success: function (res) { var result = res.resultStr; // 當needResult 為 1 時,掃碼返回的結果 _this.$router.push({ name: "reportDetail", query: { reportId: result } }); } }); }); }