简体   繁体   中英

JS import is returning syntax error

This is a bit tricky, so I'll try to explain it well.

We use an erp solution where we can create ad hoc forms. On those forms, we can program some js and use an internal API provided by our solution developers.

Now we are facing the case that several different forms have to use common data structures and functionalities (for data validation, use interaction, etc...).

We can include all the code in each form and make it work, but it seems like a poor solution. It'd probable be a lot better if we generate a js library where we export those data structures and funcionalities, so we can import them in every form and just update it in one file.

The fact is that we are receiving a syntax error when we try to import a public library, probably because on execution time, the code we type is encased into a function into our developer's code.

ourProviderObject.ourProviderMethod('formName').clientLogic = 
    function(datpar, self, editable, store) { 
        import 'https://pathToOurTestLibrary.js'; //here starts our code!!!

So... I'm wondering why the syntax error. If some operation like this (importing a file from within a function) is not allowed I'd be expecting another type of error.

It's possible to make an import from within a function?

If it is... what could be the reason behind a syntax error?

Ok... I've switched from IE to FF and the error has turned much more clarifying:

SyntaxError: import declarations may only appear at top level of a module.

So I supose that answers my question, you cannot import a file from within a function or a callback

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM