在vue專案中前端生成二維碼應該注意的問題
阿新 • • 發佈:2019-01-10
前端要生成二維碼,可以用jquery.qrcode.min.js第三方外掛來生成,並且和jquery一起用:
在使用這個外掛的時候,不能使用npm安裝,只能中需要的頁面直接引入,但是這裡有幾個細節需要注意一下:
1.在使用這個外掛的時候,要在生成二維碼方法的前面引用這個外掛,如下:
require (‘../libs/jquery.qrcode.min.js’);
this.qrcode();
如果在
mounted: function () {
this.$nextTick(function () {
// 保證 this.$el 已經插入文件
})
}
所以在呼叫這個方法時用的是
mounted: function () { //鉤子函式,等於vue1.0中的ready
this.$nextTick(function () {
require ('../libs/jquery.qrcode.min.js');
this.qrcode();
})
}
在methods裡面生成二維碼的方法非常簡單:
qrcode () {
$("#code").qrcode({
text : "http://www.baidu.com",
width:200,
height:200
});
}