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. javascript - 百度echarts series數(shù)據(jù)更新問題2. javascript - JS設(shè)置Video視頻對(duì)象的currentTime時(shí)出現(xiàn)了問題,IE,Edge,火狐,都可以設(shè)置,反而chrom卻...3. php自學(xué)從哪里開始?4. MySQL客戶端吃掉了SQL注解?5. css3 - [CSS] 動(dòng)畫效果 3D翻轉(zhuǎn)bug6. python小白的基礎(chǔ)問題 關(guān)于while循環(huán)的嵌套7. java固定鍵值轉(zhuǎn)換,使用枚舉實(shí)現(xiàn)字典?8. 求大神幫我看看是哪里寫錯(cuò)了 感謝細(xì)心解答9. phpstady在win10上運(yùn)行10. java - 我設(shè)置了cookie的max age,但是cookie依然在關(guān)閉游覽器后消失了
