背景:這幾天我在使用使用angular4作項目,因爲也是才接觸angular和typescript因此遇到很多的問題。這是剛纔解決一個的引用外部插件編譯出錯的問題。
問題描述:
使用的插件名稱:jwplayer.js
插件做用:視頻播放插件
第一步:使用npm start編譯而且開服務
Angular4使用經驗之:jwplayer插件運用到angular4插件普適法 - 877430318 - 上善若水的博客
能夠看到如今服務以及啓動成功,接下來引入jwplayer插件html
第二步:引入並使用jwplayer插件
Angular4使用經驗之:jwplayer插件運用到angular4插件普適法 - 877430318 - 上善若水的博客
能夠看到如今表示路徑不對,我把路徑改好,是否是就能夠了啦?html5
第三步:修改路徑
Angular4使用經驗之:jwplayer插件運用到angular4插件普適法 - 877430318 - 上善若水的博客
下載看來基本功能是實現了?可以播放視頻了吧。是的,在這種狀況下能夠好像是可使用插件了。
但,若是將 npm start 開的服務器關了,在重新開,看是什麼狀況。typescript
第四步,重啓服務
Angular4使用經驗之:jwplayer插件運用到angular4插件普適法 - 877430318 - 上善若水的博客
結果是找不到jwplayer,報了錯。。。
既然報了錯就想解決辦法,通常嘛想,若是在ts中引入出問題,那我直接在html中使用script行嗎npm
第五步,在html中使用script引入
Angular4使用經驗之:jwplayer插件運用到angular4插件普適法 - 877430318 - 上善若水的博客
能夠看到,雖然編譯的使用沒有報錯了,把服務給開起來了,可是在<div id="player-container"></div>這個標籤裏面什麼都沒有,也就是jwplayer.js其實沒有被引入進來,這個服務器
<script src="app/assets/lib/jwplayer/jwplayer.js"></script> <script> jwplayer('player-container').setup({ flashplayer: 'app/assets/lib/jwplayer/jwplayer.flash.swf', file: 'http://scvideo.sheencity.com/smart/Mars-M-01.mp4', image: 'app/assets/img/player-cover.jpg', width: '100%', autostart: 'false', controlbar: 'bottom', primary: "html5", aspectratio: '10:6', dock: false }); </script>
代碼什麼事也沒幹,就想沒有它同樣。
既然這樣不行,我就繼續想辦法,我就想如今是這個script標籤沒有效果,若是它生效那是否是這個播放插件就能夠用了?app
第六步:讓script標籤生效
注意:如今在jwplayer.js中的路徑'jwplayer.html5.js',我又把它改回來了,否則路徑又不對頭了。總之就是這樣就是對的了,不用去改jwplayer原生代碼。
並且重啓服務也能夠的。。。
關鍵代碼:angular4
/** * 使用計時器,解決插件使用問題 */ var oScript1 = document.createElement("script"), oScript2 = document.createElement("script"); oScript1.src = "app/assets/lib/jwplayer/jwplayer.js"; oScript2.innerHTML = ` var timer = setInterval(function() { if(window.jwplayer) { /** * 首頁視屏播放 */ var player = jwplayer('player-container').setup({ flashplayer: 'app/assets/lib/jwplayer/jwplayer.flash.swf', file: 'http://scvideo.sheencity.com/smart/Mars-M-01.mp4', image: 'app/assets/img/player-cover.jpg', width: '100%', height: '100%', autostart: 'false', controlbar: 'bottom', primary: "html5", aspectratio: '10:6', dock: false }); clearInterval(timer); } }, 30); `; document.body.appendChild(oScript1); document.body.appendChild(oScript2);
總結:jwplayer插件能夠經過這種方法獲得使用,那其它的插件確定就也是能夠的哪,好吧一個新手在這兒自沾自喜。。
終於搞完了,若是哪兒不對頭,或者有其它好方法,請你們必定給我說說。。謝謝哈ide