I already spent more than a day searching for a solution. I am new to JavaScript, so maybe I missed the solutions adressed to experienced JS devs. The 3rd party script I need to use in a single file vue component (or globally in my app if that's the only way) has the following pattern:
(function (win) {
win.MyUtil = {
"func1": function func1() { ... },
"func2": function func1() { ... }
}
}(window));
This extends the browser's Window object, so that MyUtil is globally visible, right?
Then I added a script tag to my index.html file's head:
The script file was in /my_project_folder/src/assets. I also tried many different paths in the src attribute (e.g. "./assets/my_util.js" or "<%= BASE_URL %>/assets/my_util.js" and moved the file to different actual folders. I read somewhere there should be a folder "/my_project/static". I tried that.
The result was always the same. The browser shows this warning (translated to English myself): "The script 'http://localhost:8080/@/assets/my_util.js' was loaded, although its MIME type (text/html) was not a valid MIME type for JavaScript." I think this indicates that the script was actually loaded. Of course I also tried with specifying the correct MIME type without success. However when I add alert("my_util") to the script, no message is shown.
The code in my vue component then throws an error "ReferenceError: MyUtil is not defined". This happens in the "mounted" hook, but also later in a button click, so it is not a matter of loading order.
What is going wrong here? How can I fix it?
By the way it works fine in plain html.
<script>tag? And I suppose you are using VueCLI?MyUtil: separate propertiesfunc1,func2with a,.import my_util from '@/assets/my_util.js'