3

I have a basic project in VS code, and quite a simple task. I want to include an old javascript file in my project the correct way, so it gets loaded in the browser.
The file should be located in src\assets\scripts\oldLegacyScript.js

I tried this hack: How to add external JS scripts to VueJS Components which injects a <source> tag in the document on runtime. This only works if I put the .js file in the generated public folder in where the compiled files will be. If the file is not in the public folder the browser tries to download the index.html file, which I cannot understand:

file not found enter image description here

If i follow this solution: Importing javascript file for use within vue component I get syntax errors on the import statement: enter image description here

So how the heck do I overcome this simple task of importing a simple javascript file in my Vue project?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Farsen
  • 1,387
  • 3
  • 21
  • 48
  • If you see the content of your index.html, then it means that importing wasn’t successful, and maybe the default route can solve the problem. But it cannot, of course, so it’s an error. – muka.gergely Apr 05 '19 at 08:27
  • Maybe the syntax here? https://stackoverflow.com/questions/41179828/es6-import-equivalent-of-require-without-exports – muka.gergely Apr 05 '19 at 08:34

1 Answers1

7

Import like this

<script>
import * as myKey from '.src/..';

export default {

}
</script>
Tony Tom
  • 1,435
  • 1
  • 10
  • 17
  • 1
    This answer may be OK, if that script exports something. If it’s really legacy code (I mean old), it doesn’t export anything. – muka.gergely Apr 05 '19 at 08:28
  • Well using that syntax is actually working! The script is loaded into the browsers memory. Problem is just that the content inside the script creates a globally accessible variable which Vue can´t handle or access. But the question is answered correctly, so thanks :) – Farsen Apr 08 '19 at 20:23