将脚本添加到vue模板内部

时间:2018-07-25 21:26:55

标签: javascript vuejs2

我需要这样的插入脚本

    <div data-player-id="912d05c">
  <script src="//cdn.flowplayer.com/players/7/flowplayer.async.js">
    { 
            "src": "https://s3.amazonaws.com/69693f173770c49cbb5.mp4"
    }
  </script>
</div>

移至Vue下方的html内部。

所以我发现我需要通过js生成script标记,但是我不确定如何添加

{ 
        "src": "https://s3.amazonaws.com/69693f173770c49cbb5.mp4"
}

script标签

输入我的代码(简体):

<div id="app">
    <div id="videocontent"></div>
</div>

el: "#app",
data: {},
created: function() {
    let playerContainer = document.createElement('div');
    playerContainer.setAttribute('data-player-id','912d05c');
    let flowplayerScript = document.createElement('script');
    flowplayerScript.setAttribute('src', '//cdn.flowplayer.com/players/7/flowplayer.async.js');
    flowplayerScript.innerText = {"src": "https://s3.amazonaws.com/productionadgate_video/eceae5886caaf69693f173770c49cbb5.mp4"};

    playerContainer.append(flowplayerScript);
    let container = document.getElementById('videocontent');
    container.append(playerContainer);
}

flowplayerScript.innerText = {"src": "https://s3.amazonaws.com/productionadgate_video/eceae5886caaf69693f173770c49cbb5.mp4"};未正确插入,播放器始终加载但不显示视频。我也尝试过使用:

flowplayerScript.onload = function(){
     return {
         "src": "https://s3.amazonaws.com/productionadgate_video/eceae5886caaf69693f173770c49cbb5.mp4"
        }
};

但仍然不起作用:(而且出现类似以下错误:

SyntaxError: Unexpected token $ in JSON at position 0 flowplayer.async.js:2

1 个答案:

答案 0 :(得分:1)

您可以使用纯JavaScript安装,然后以“ mount”方法初始化flowplayer。

new Vue({
  el: "#app",
  mounted: function() {
    this.$nextTick(function() {
      // select the above element as player container
      let containerEl = document.getElementById("videocontent")

      // install flowplayer into selected container
      flowplayer(containerEl, {
        clip: {
          sources: [
            { type: "application/x-mpegurl",
              src:  "//mydomain.com/video.m3u8" },
            { type: "video/mp4",
              src:  "//mydomain.com/video.mp4" }
          ]
        }
      })
    })
  }
})

jsfiddle