Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Dynamic imports#205
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
80 changes: 40 additions & 40 deletions1-js/13-modules/03-modules-dynamic-imports/article.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,98 @@ | ||
#Dynamické importy | ||
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í. | ||
Za prvé, žádné parametry příkazu`import` nemůžeme dynamicky generovat. | ||
Cesta k modulu musí být primitivní řetězec, ne volání funkce. Tohle nebude fungovat: | ||
```js | ||
import ... from *!*vraťNázevModulu()*/!*; //Chyba, povoleno je jenfrom "řetězec" | ||
``` | ||
Za druhé, nemůžeme importovat podmíněně nebo za běhu skriptu: | ||
```js | ||
if(...) { | ||
import ...; //Chyba, tohle není dovoleno! | ||
} | ||
{ | ||
import ...; //Chyba, nemůžeme umístitimportdo bloku | ||
} | ||
``` | ||
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á. | ||
Jak ale můžeme importovat modul dynamicky, na požádání? | ||
##Výraz import() | ||
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. | ||
Můžeme jej používat dynamicky na kterémkoli místě kódu, například: | ||
```js | ||
letcestaModulu = prompt("Který modul načíst?"); | ||
import(cestaModulu) | ||
.then(obj => <objekt modulu>) | ||
.catch(chyba => <chyba při načítání, např. když takový modul neexistuje>) | ||
``` | ||
Nebo můžeme použít`letmodul = await import(cestaModulu)`, jsme-li uvnitř asynchronní funkce. | ||
Například jestliže máme následující modul `řekni.js`: | ||
```js | ||
// 📁řekni.js | ||
export functionahoj() { | ||
alert(`Ahoj`); | ||
} | ||
export functionnashle() { | ||
alert(`Nashle`); | ||
} | ||
``` | ||
...Pak dynamický importmůže vypadat takto: | ||
```js | ||
let {ahoj, nashle} = await import('./řekni.js'); | ||
ahoj(); | ||
nashle(); | ||
``` | ||
Nebo jestliže `řekni.js`obsahuje výchozí export: | ||
```js | ||
// 📁řekni.js | ||
export default function() { | ||
alert("Modul načten (výchozí export)!"); | ||
} | ||
``` | ||
...Pak můžeme pro přístup k němu použít vlastnost`default`objektu modulu: | ||
```js | ||
let obj = await import('./řekni.js'); | ||
letřekni = obj.default; | ||
//nebo na jednom řádku: let {default:řekni} = await import('./řekni.js'); | ||
řekni(); | ||
``` | ||
Zde je celý příklad: | ||
[codetabs src="say" current="index.html"] | ||
```smart | ||
Dynamické importy fungují i v běžných skriptech, nevyžadují `script type="module"`. | ||
``` | ||
```smart | ||
Ačkoli `import()`vypadá jako volání funkce, je to speciální syntaxe, která prostě jen používá závorky (podobně jako `super()`). | ||
Nemůžeme tedy kopírovat`import`do proměnné nebo s ním používat`call/apply`. Není to funkce. | ||
``` |
12 changes: 6 additions & 6 deletions1-js/13-modules/03-modules-dynamic-imports/say.view/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<!doctype html> | ||
<script> | ||
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="načti()">Klikni na mě</button> |
10 changes: 5 additions & 5 deletions1-js/13-modules/03-modules-dynamic-imports/say.view/say.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
export functionahoj() { | ||
alert(`Ahoj`); | ||
} | ||
export functionnashle() { | ||
alert(`Nashle`); | ||
} | ||
export default function() { | ||
alert("Modul načten (výchozí export)!"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.