[picture] how can vue introduce anonymous functions?

question

vue create a test.js

in assets\ js\ test.js .
al = function(s){alert(s)}(window, document, XMLHttpRequest)
export {al};

how do I introduce this js in the .vue file?

or the requirement is how vue introduces OpenInstall

Mar.16,2021

this is actually the same as importing components. You only need to import {al} from'/ * your path*/assets\ js\ test.js' in the script tag of the .vue file, which has nothing to do with vue . It's a es6 module


.
export default al
< hr >
import al from 'XXXXX';

everything above is true, I add a premise: your browser needs to support ES6 Modules; or you need to use webpack for packaging.


first of all, your al does not declare that you use babel when you package with webpack. This tool will use strict mode to process your source code by default.
in strict mode, variables are used without declaration, which is probably the most direct reason for al is not defined
disabling strict mode: https://codeshelper.com/q/10.
even in non-strict mode:
when you see al, Al has been assigned the following code
function (s) {alert (s)} (window, document, XMLHttpRequest)
, and the above code is a self-executing function that evaluates. Its value is the return value of the function (s) {alert (s)} function. If you do not write the return value, it returns undefined, by default, so you have exported undefined
. That should be what it is

.
ES6's module automatically adopts strict mode, whether or not you add "use strict" to the module header;

look at it like this

var al = function(s){alert(s)}(window, document, XMLHttpRequest)
export {al};

in your .vue component

<script>
import {al} from "test.js" 
console.log(al) //undefined
<script>
Menu