5

I need to import a file "js" from folder "public" in a vue component.

my Vue component:

<template>
  <div>
    <h2>Hello</h2>
  </div>
</template>
<script>

   import {myplugin} from "/public/vendor/jquery/jquery.min.js";
   export default {
      name: "pageone"
   }
</script>

but i receive an error in compiling phase, can someone help me?

4
  • what is the error? Please attach error screenshot. Commented Jan 1, 2020 at 14:24
  • You need to use relative path, I assume. Something like from '../../../public/vendor/jquery/jquery.min.js' Commented Jan 1, 2020 at 14:25
  • Why is there a vendor folder in your public folder? jquery is most likely also present already (depending on which version of Laravel/Laravel UI you're using). Include/require jquery via npm. laravel.com/docs/master/mix#working-with-scripts Commented Jan 1, 2020 at 14:27
  • Also, this import {myplugin} from is wrong. Should be something like import * as jQuery from ... Commented Jan 1, 2020 at 14:31

2 Answers 2

4

(Although it's not a very good idea to place your libraries/packages to public folder when using compilers), if you really want to place it in public folder, you should use a relative_path.

import {myplugin} from "../../public/vendor/jquery/jquery.min.js";

However, I'd install it as npm package and leverage laravel-mix for adding jquery in my build.

Sign up to request clarification or add additional context in comments.

Comments

-1

No need to import it.

Just add a <script src="/path/to/your.js"> into head section of public/index.html.

Comments

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.