jeudi 27 mai 2021

How to use * to import in JavaScript?

I have a web application where I use it, as in the example below.

import * as b from './library/library.js';

Everything is working as it should in Windows, Linux, Android with Chrome and Firefox, but I noticed that it wasn’t working on Safari on the Iphone and Mac, it also didn’t work on iPhone's Chrome, all of them.

I looked without a console and was returning this error.

SyntaxError: Unexpected identifier 'as'. Expected 'from' before exported module name.

I did some tests and found that the problem only happens when using the asterisk * to import all the functions of the module at once, as in the example below;

import * as name from "module-name"; // This way of the error.

import {export} from "module-name"; // That works OK.

Does anyone know how to explain the problem in Safari, or if there is a solution?

I need to import in this generic way, because there are many small functions that I export this library in addition to using it on all pages of the System, so it wouldn’t be very practical to have to name each function that I’m importing on each page of the system.

I already appreciate who can help.




Aucun commentaire:

Enregistrer un commentaire