javascript - vue組件中使用百度分享初次加載失敗?
問題描述
我的vue項(xiàng)目中使用了百度分享,代碼如下template中js(放在created中)
初次加載時(shí),百度分享的按鈕圖片都不顯示,F(xiàn)12發(fā)現(xiàn)class和點(diǎn)擊事件都未生效刷新后就正常顯示使用
在mounted中使用this.$nextTick,也有點(diǎn)問題
mounted() {
let _this = this;this.$nextTick(function () { window._bd_share_config = {common : { bdText : _this.art.title, bdDesc : _this.art.abstract, bdPic : 'http://www.jubao56.com/upload_admin/2016-07-05%2010:08:50-banner4_m.jpg',},share : [{ tag:'share_0', bdSize : 24}], }; const s = document.createElement(’script’); s.type = ’text/javascript’; s.src = ’http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=’ + ~(-new Date() / 36e5); document.body.appendChild(s);}) },
問題解答
回答1:js寫在 mounted事件里,this.$nextTick(() => { ...... });
百度分享的 js可能會(huì) 立即執(zhí)行并查找DOM元素。created事件觸發(fā)時(shí),并不存在DOM
相關(guān)文章:
1. 數(shù)組按鍵值封裝!2. docker不顯示端口映射呢?3. java - 阿里的開發(fā)手冊(cè)中為什么禁用map來作為查詢的接受類?4. win10 python3.5 matplotlib使用報(bào)錯(cuò)5. python - django 使用 redirect 跳轉(zhuǎn)網(wǎng)頁,怎么傳遞 referer 給目標(biāo)網(wǎng)頁6. python - flask用ajax做后臺(tái)登陸交互,驗(yàn)證密碼后,如何重新定位到index.html上?7. python - 如何找第三方編譯好的模塊?8. python - SQLAlchemy 向?qū)?yīng)的id插入數(shù)據(jù)怎么操作?9. mysql 新增用戶 主機(jī)名設(shè)定 失敗10. python中def定義的函數(shù)加括號(hào)和不加括號(hào)的區(qū)別?
