Cannot read properties of undefined (reading 'appendChild')
时间: 2023-09-29 14:02:32 浏览: 105
这个错误是因为在调用this.$QRCode时,没有正确使用appendChild函数。根据引用的描述,这个错误可能是因为在created生命周期中,Vue还没有生成DOM,所以无法找到this.$refs.qrCodeUrl对应的DOM元素。要解决这个问题,可以根据引用的建议,在创建二维码之前使用setTimeout函数来延迟执行,以确保Vue已经生成了DOM。代码示例如下:
```javascript
creatQrCode() {
var that = this;
setTimeout(() => {
var qrcode = new that.$QRCode(that.$refs.qrCodeUrl, {
text: '5201314', // 需要转换为二维码的内容
width: 100,
height: 100,
colorDark: '#000000',
colorLight: '#ffffff',
correctLevel: that.$QRCode.CorrectLevel.H
});
}, 0);
},
```
通过将创建二维码的代码放在setTimeout函数中,将其延迟执行,确保Vue已经生成DOM,从而避免了"Cannot read properties of undefined (reading 'appendChild')"错误的出现。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Cannot read properties of undefined (reading ‘appendChild‘)](https://blog.csdn.net/chen_XiaoFang/article/details/125010507)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* [qrcodejs2报错 TypeError: Cannot read properties of undefined (reading ‘appendChild‘)](https://blog.csdn.net/lornall/article/details/130124422)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]