最近开发一个项目,需要后台返回一个二维码,所以后台返回给我一串base64格式的字符串。本以为直接img标签渲染即可,但死活不出来,以为我的方法有问题,一直在查找。后来发现是后台返回的base64图片格式的字符串 带有换行和空格,写了一个方法去掉换行和空格,图片立马渲染成功(吐槽一下。。)
getQRcode () {
let _this = this
this.$http.get('/api/xxxxxxxxxxxxx/qrcode', {}).then((res) => {
if (res) {
// 后台返回的base64图片格式去掉空格和换行
let str = res.replace(/\. +/g, '')
str = str.replace(/[\r\n]/g, '')
_this.qrcode = str
} else {
console.log('图片不存在')
}
}).catch(err => {
console.log(err.status, err.message)
})
}