wx.createSelectorQuery().in(this).select(’#’ + id);
Taro小程序同理
const query = wx.createSelectorQuery().in(this);
query
.select(`#${id}`)
.fields({
node: true,
size: true,
})
.exec((res) => {
console.log('获取的res对象',res)
});
});
然而 res 得到的是null,这里提供两种解决方式
一、添加 in(this)
const query = wx.createSelectorQuery().in(this);
query
.select(`#${id}`)
.fields({
node: true,
size: true,
})
.exec((res) => {
const canvas = res[0].node;
// 调用方法drawQrcode生成二维码
drawQrcode({
canvas: canvas,
id: id,
width: width,
padding: padding,
background: background,
foreground: foreground,
text: data,
});
});
二、如果通过wx.createSelectorQuery().in(that)获取不到节点,可以试试通过定时器来获取
(setTimeout在onLoad里面写)
setTimeout(()=>{
wx.createSelectorQuery().in(this).select('#' + id);
},200)