Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dynamic imports#205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
otmon76 merged 1 commit intojavascript-tutorial:masterfromotmon76:1.13.3
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 40 additions & 40 deletions1-js/13-modules/03-modules-dynamic-imports/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,98 @@
#Dynamic imports
#Dynamické importy

Export and import statements that we covered in previous chapters are called "static". The syntax is very simple and strict.
Příkazy exportu a importu, které jsme uvedli v předchozích kapitolách, se nazývají „statické“. Jejich syntaxe je velmi jednoduchá a striktní.

First, we can't dynamically generate any parameters of`import`.
Za prvé, žádné parametry příkazu`import` nemůžeme dynamicky generovat.

The module path must be a primitive string, can't be a function call. This won't work:
Cesta k modulu musí být primitivní řetězec, ne volání funkce. Tohle nebude fungovat:

```js
import ... from *!*getModuleName()*/!*; //Error, onlyfrom "string" is allowed
import ... from *!*vraťNázevModulu()*/!*; //Chyba, povoleno je jenfrom "řetězec"
```

Second, we can't import conditionally or at run-time:
Za druhé, nemůžeme importovat podmíněně nebo za běhu skriptu:

```js
if(...) {
import ...; //Error, not allowed!
import ...; //Chyba, tohle není dovoleno!
}

{
import ...; //Error, we can't putimportin any block
import ...; //Chyba, nemůžeme umístitimportdo bloku
}
```

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.
Je to proto, že záměrem příkazů`import`/`export`je poskytnout páteř struktury kódu. To je dobrá věc, protože strukturu kódu můžeme analyzovat, moduly můžeme speciálními nástroji shromažďovat a spojovat do jednoho souboru, nepoužité exporty můžeme odstraňovat („třesení stromem“).To je možné jen proto, že struktura importů a exportů je jednoduchá a pevná.

But how can we import a module dynamically, on-demand?
Jak ale můžeme importovat modul dynamicky, na požádání?

##The import() expression
##Výraz import()

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.
Výraz `import(modul)`načte modul a vrátí příslib, který se splní do objektu modulu obsahujícího všechny jeho exporty. Může být volán z kteréhokoli místa v kódu.

We can use it dynamically in any place of the code, for instance:
Můžeme jej používat dynamicky na kterémkoli místě kódu, například:

```js
letmodulePath = prompt("Which module to load?");
letcestaModulu = prompt("Který modul načíst?");

import(modulePath)
.then(obj => <module object>)
.catch(err => <loading error, e.g. if no such module>)
import(cestaModulu)
.then(obj => <objekt modulu>)
.catch(chyba => <chyba při načítání, např. když takový modul neexistuje>)
```

Or, we could use`letmodule = await import(modulePath)` if inside an async function.
Nebo můžeme použít`letmodul = await import(cestaModulu)`, jsme-li uvnitř asynchronní funkce.

For instance, if we have the following module `say.js`:
Například jestliže máme následující modul `řekni.js`:

```js
// 📁say.js
export functionhi() {
alert(`Hello`);
// 📁řekni.js
export functionahoj() {
alert(`Ahoj`);
}

export functionbye() {
alert(`Bye`);
export functionnashle() {
alert(`Nashle`);
}
```

...Then dynamic importcan be like this:
...Pak dynamický importmůže vypadat takto:

```js
let {hi, bye} = await import('./say.js');
let {ahoj, nashle} = await import('./řekni.js');

hi();
bye();
ahoj();
nashle();
```

Or, if `say.js`has the default export:
Nebo jestliže `řekni.js`obsahuje výchozí export:

```js
// 📁say.js
// 📁řekni.js
export default function() {
alert("Module loaded (export default)!");
alert("Modul načten (výchozí export)!");
}
```

...Then, in order to access it, we can use`default`property of the module object:
...Pak můžeme pro přístup k němu použít vlastnost`default`objektu modulu:

```js
let obj = await import('./say.js');
letsay = obj.default;
//or, in one line: let {default:say} = await import('./say.js');
let obj = await import('./řekni.js');
letřekni = obj.default;
//nebo na jednom řádku: let {default:řekni} = await import('./řekni.js');

say();
řekni();
```

Here's the full example:
Zde je celý příklad:

[codetabs src="say" current="index.html"]

```smart
Dynamic imports work in regular scripts, they don't require `script type="module"`.
Dynamické importy fungují i v běžných skriptech, nevyžadují `script type="module"`.
```

```smart
Although `import()`looks like a function call, it's a special syntax that just happens to use parentheses (similar to `super()`).
Ačkoli `import()`vypadá jako volání funkce, je to speciální syntaxe, která prostě jen používá závorky (podobně jako `super()`).

So we can't copy`import`to a variable or use`call/apply` with it. It's not a function.
Nemůžeme tedy kopírovat`import`do proměnné nebo s ním používat`call/apply`. Není to funkce.
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
<!doctype html>
<script>
async functionload() {
letsay = await import('./say.js');
say.hi(); //Hello!
say.bye(); //Bye!
say.default(); //Module loaded (export default)!
async functionnačti() {
letřekni = await import('./say.js');
řekni.ahoj(); //Ahoj!
řekni.nashle(); //Nashle!
řekni.default(); //Modul načten (výchozí export)!
}
</script>
<button onclick="load()">Click me</button>
<button onclick="načti()">Klikni na mě</button>
10 changes: 5 additions & 5 deletions1-js/13-modules/03-modules-dynamic-imports/say.view/say.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
export functionhi() {
alert(`Hello`);
export functionahoj() {
alert(`Ahoj`);
}

export functionbye() {
alert(`Bye`);
export functionnashle() {
alert(`Nashle`);
}

export default function() {
alert("Module loaded (export default)!");
alert("Modul načten (výchozí export)!");
}

[8]ページ先頭

©2009-2025 Movatter.jp