简体   繁体   中英

Import javascript file in Vue component

i'm trying to import a .js file into one of my Vue components.

I've already tryed : import './magnetic' but i got this error:

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted.

If i put it like this <script src="magnetic.js"></script> into index.html it work without problems.

You can find the .js i'm using at this link: https://github.com/codrops/MagneticButtons/blob/master/dist/demo1.151408fb.js

Can someone help me please?

Use mounted hook to load your js file

<script>
export default {
  mounted() {
    const plugin = document.createElement("script");
    plugin.setAttribute(
      "src",
      "//api.example.com/widget/core.js"
    );
    plugin.async = true;
    document.head.appendChild(plugin);
  }
};
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM