You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Export and import statements that we covered in previous chapters are called "static". The syntax is very simple and strict.
3
+
Le istruzioni di export ed import che abbiamo visto nei capitolo precedente sono detti "statici". La sintassi è molto semplice e rigorosa.
4
4
5
-
First, we can't dynamically generate any parameters of`import`.
5
+
Come prima cosa, non possiamo generare dinamicamente parametri di`import`.
6
6
7
-
The module path must be a primitive string, can't be a function call. This won't work:
7
+
Il percorso al modulo deve essere una stringa, non può essere una chiamata a funzione. Questo non funzionerebbe:
8
8
9
9
```js
10
-
import ... from*!*getModuleName()*/!*; //Error, only from "string" is allowed
10
+
import ... from*!*getModuleName()*/!*; //Errore, sono ammesse solamente string
11
11
```
12
12
13
-
Second, we can't import conditionally or at run-time:
13
+
Secondo, non possiamo importare a run-time in base a determinate condizioni:
14
14
15
15
```js
16
16
if(...) {
17
-
import ...; //Error, not allowed!
17
+
import ...; //Errore, non è possibile farlo!
18
18
}
19
19
20
20
{
21
-
import ...; //Error, we can't put import in any block
21
+
import ...; //Errore, non possiamo scrivere gli import all'interno di nessun blocco
22
22
}
23
23
```
24
24
25
-
That's because `import`/`export`aim to provide a backbone for the code structure. That's a good thing, as code structure can be analyzed, modules can be gathered and bundled into one file by special tools, unused exports can be removed ("tree-shaken"). That's possible only because the structure of imports/exports is simple and fixed.
25
+
Questo accade perché `import`/`export`mirano a fornire uno scheletro per la struttura del codice. Questa è una buona cosa, poiché la struttura del codice può essere analizzata, i moduli possono essere raccolti and impacchettati in un singolo file (grazie ad alcuni strumenti) e gli export inutilizzati possono essere rimossi ("tree-shaken"). Questo è possibile solamente perché la struttura degli imports/exports è semplice e preimpostata.
26
26
27
-
But how can we import a module dynamically, on-demand?
27
+
Ma come possiamo importare un modulo dinamicamente, a seconda delle necessità?
28
28
29
-
## The import() expression
29
+
## L'espressione import()
30
30
31
-
The`import(module)`expression loads the module and returns a promise that resolves into a module object that contains all its exports. It can be called from any place in the code.
31
+
L'espressione`import(module)`carica il modulo e ritorna una promise, che si risolve in un oggetto che contiene tutti gli export del modulo. Può essere quindi invocata in un qualsiasi punto del codice.
32
32
33
-
We can use it dynamically in any place of the code, for instance:
33
+
Possiamo utilizzarla dinamicamente ovunque, ad esempio:
34
34
35
35
```js
36
36
let modulePath =prompt("Which module to load?");
@@ -40,9 +40,9 @@ import(modulePath)
40
40
.catch(err=><loading error, e.g. if no such module>)
41
41
```
42
42
43
-
Or, we could use `let module = await import(modulePath)`if inside an async function.
43
+
oppure, potremmo utilizzare `let module = await import(modulePath)`se ci troviamo all'interno di una funzione asincrona.
44
44
45
-
For instance, if we have the following module`say.js`:
45
+
Ad esempio, se abbiamo il seguente modulo`say.js`:
46
46
47
47
```js
48
48
// 📁 say.js
@@ -55,7 +55,7 @@ export function bye() {
55
55
}
56
56
```
57
57
58
-
...Then dynamic import can be like this:
58
+
...Allora il dyamic import può essere scritto così:
59
59
60
60
```js
61
61
let {hi, bye} =awaitimport('./say.js');
@@ -64,7 +64,7 @@ hi();
64
64
bye();
65
65
```
66
66
67
-
Or, if`say.js`has the default export:
67
+
Oppure, se`say.js`ha un default export:
68
68
69
69
```js
70
70
// 📁 say.js
@@ -73,26 +73,26 @@ export default function() {
73
73
}
74
74
```
75
75
76
-
...Then, in order to access it, we can use `default`property of the module object:
76
+
...Quindi, per potervi accedere, possiamo utilizzare la proprietà `default`dell'oggetto:
77
77
78
78
```js
79
79
let obj =awaitimport('./say.js');
80
80
let say =obj.default;
81
-
//or, in one line: let {default: say} = await import('./say.js');
81
+
//o, in una riga: let {default: say} = await import('./say.js');
82
82
83
83
say();
84
84
```
85
85
86
-
Here's the full example:
86
+
Qui vediamo l'esempio completo:
87
87
88
88
[codetabs src="say" current="index.html"]
89
89
90
90
```smart
91
-
Dynamic imports work in regular scripts, they don't require `script type="module"`.
91
+
I dynamic import funzionano negli script regolari, non richiedono `script type="module"`.
92
92
```
93
93
94
94
```smart
95
-
Although `import()` looks like a function call, it's a special syntax that just happens to use parentheses (similar to `super()`).
95
+
Anche se `import()` sembra una chiamata a funzione, in realtà è una speciale sintassi che utilizza le parentesi (in modo simile a `super()`).
96
96
97
-
So we can't copy `import` to a variable or use `call/apply` with it. It's not a function.
97
+
Quindi non possiamo copiare `import` in una variabile o utilizzare `call/apply`. Non è una funzione.
0 commit comments