0

I have previously made some drag and drop functionality in vanilla JS which I have used in other projects. Now I have started a Vue.js project and I would like to use the same drag and drop functionality.

Is it possible to include a vanilla JS file in a Vue.js component? And how can it be done?

So far I have only tried to add a <script> tag in the head element in the index.html but it throws an error.

<script src="../src/js/drag-and-drop.js"></script>
5
  • What kind of error? Can you also share your code? Commented Dec 19, 2018 at 9:49
  • Just a "Uncaught SyntaxError: Unexpected token <" But that shows up no matter if the file is empty or not, I dont think that there is an issue with the file, just that it is imported or included in a wrong way. Commented Dec 19, 2018 at 9:50
  • Can you show how do you import it? Commented Dec 19, 2018 at 10:01
  • <script src="../src/js/drag-and-drop.js"></script> Commented Dec 19, 2018 at 10:14
  • Check this answer Commented Dec 19, 2018 at 10:23

1 Answer 1

9

You can import the script within your vue component with either

  1. import

    import '../src/js/drag-and-drop.js';

  2. require

    require('../src/js/drag-and-drop.js');

in the script section of your component.

e.g.

<template>
  <!-- vue component markup -->
</template>

<script> 
import drag from '../src/js/drag-and-drop';
export default {
  name: 'you-vue-component',
}    
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Yes true! A good example is given stackoverflow.com/questions/43682446/…
let me try this on my codebase

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.