在vue.js模板中包含外部脚本 [英] Including external script in vue.js template

查看:56
本文介绍了在vue.js模板中包含外部脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Vue.js和web-pack的新手,所以我决定使用vue-cli(webpack)来构建一个初始应用程序。我试图在一个全局不需要的模板中包含一个外部脚本(例如< script src =...)(对于每个页面/组件)但是,Vue警告说这是不允许的。

I'm new to Vue.js and web-pack, so I decided to use the vue-cli (webpack) to scaffold an initial application. I'm trying to include an external script (e.g <script src="...") in a template which isn't needed globally (for every page/component), however Vue warns that this isn't allowed.

我的index.html文件类似于最初生成的文件:

My index.html file is similar to the initial generated one:

<html lang="en">

<head>
  <title>App</title>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

</head>

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

  <!-- jQuery first, then Tether, then Bootstrap JS. -->
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</body>

</html>

我的App.vue也与生成的类似:

My App.vue is also similar to the generated one:

<template>
<div id="app">

  <div class="container pt-5">
    <router-view></router-view>
  </div>

</div>
</template>

我有一条去往 / upload 的路线我的路由文件,映射到需要dropzone.js(外部脚本)的上传组件。我可以将它包含在我的index.html中,类似于如何加载引导程序,但是当只有这个组件需要它时,为每个页面/组件加载它似乎不太理想。

I have a route to /upload in my routes file, which maps to an Upload component which requires dropzone.js (an external script). I could include it in my index.html, similarly to how bootstrap is loaded, however it seems less than ideal to load it for every page/component when only this component requires it.

但是,如上所述,我只是将它包含在我的模板文件中:

However, as stated above, I simply cannon include it in my template file as such:

<template>
<div>
  <h2>Upload Images</h2>
  <form action="/file-upload" class="dropzone">
    <div class="fallback">
      <input name="file" type="file" multiple />
      <input type="submit" value="upload" />
    </div>
  </form>
</div>

<script src="https://example.com/path/to/dropzone"></script>
</template>

<script>
export default {
  data() {
    return {}
  }
}
</script>

<style>  
</style>

如何只为一个组件包含外部脚本?

How can I include an external script for only one component?

推荐答案

您可以定义一个负责脚本加载的方法,并在挂载或创建的挂钩中调用它,如下所示: / p>

You can define a method that will be responsible for script loading and call it in the mounted or created hook like below:

<script>
      export default {
        data() {
          return {}
        },
        methods: {
          loadJs(url, callback) {
            jQuery.ajax({
              url: url,
              dataType: 'script',
              success: callback,
              async: true
            });
          }
        },
        mounted() {
          this.loadJs('url_to_someScript.js', function() {
            //Stuff to do after someScript has loaded
          });
        }
      }
</script>

这篇关于在vue.js模板中包含外部脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆