mirror of https://github.com/sveltejs/svelte
parent
0c7b30603e
commit
bac68f5d3d
@ -0,0 +1,66 @@
|
|||||||
|
---
|
||||||
|
title: Module context
|
||||||
|
---
|
||||||
|
|
||||||
|
So far, our `<script>` tags have been running in the context of a component *instance*. In other words, if you have two components like this...
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!-- { title: 'Counter' } -->
|
||||||
|
<script>
|
||||||
|
import Counter from './Counter.html';
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Counter/>
|
||||||
|
<Counter/>
|
||||||
|
```
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!--{ filename: 'Counter.html' }-->
|
||||||
|
<script>
|
||||||
|
let count = 0;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<button on:click="{() => count += 1}">+1</button>
|
||||||
|
```
|
||||||
|
|
||||||
|
...each counter has its own `count` variable. The code runs once per instance.
|
||||||
|
|
||||||
|
Occasionally, you want code to run once *per module* instead. For that, we use `context="module"`:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script context="module">
|
||||||
|
console.log(`this will run once`);
|
||||||
|
const answer = 42;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
console.log(`this will run once per instance`);
|
||||||
|
console.log(`we can 'see' module-level variables like ${answer}`);
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
> Don't worry about manually hoisting functions from instance context to module context to avoid creating multiple copies of them — Svelte will do that for you
|
||||||
|
|
||||||
|
|
||||||
|
### Module exports
|
||||||
|
|
||||||
|
Any named exports from a `context="module"` script become part of the module's static exports. For example, to define a `preload` function for use with [Sapper](https://sapper.svelte.technology):
|
||||||
|
|
||||||
|
```html
|
||||||
|
<!-- { title: 'Module exports', repl: false } -->
|
||||||
|
<script context="module">
|
||||||
|
export async function preload({ params }) {
|
||||||
|
const res = await this.fetch(`/blog/${params.slug}.json`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
post: await res.json()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
import BlogPost, { preload } from './BlogPost.html';
|
||||||
|
```
|
||||||
|
|
||||||
|
You can only have named exports — no `export default` — because the component *is* the default export.
|
@ -1,37 +0,0 @@
|
|||||||
---
|
|
||||||
title: Static properties
|
|
||||||
---
|
|
||||||
|
|
||||||
|
|
||||||
### Setup
|
|
||||||
|
|
||||||
In some situations, you might want to add static properties to your component constructor. For that, we use the `setup` property:
|
|
||||||
|
|
||||||
```html
|
|
||||||
<!-- { title: 'Component setup' } -->
|
|
||||||
<p>check the console!</p>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
export default {
|
|
||||||
setup(MyComponent) {
|
|
||||||
// someone importing this component will be able
|
|
||||||
// to access any properties or methods defined here:
|
|
||||||
//
|
|
||||||
// import MyComponent from './MyComponent.html';
|
|
||||||
// console.log(MyComponent.ANSWER); // 42
|
|
||||||
MyComponent.ANSWER = 42;
|
|
||||||
},
|
|
||||||
|
|
||||||
oncreate() {
|
|
||||||
console.log('the answer is', this.constructor.ANSWER);
|
|
||||||
console.dir(this.constructor);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
|
||||||
```
|
|
||||||
|
|
||||||
### preload
|
|
||||||
|
|
||||||
If your component definition includes a `preload` function, it will be attached to the component constructor as a static method. It doesn't change the behaviour of your component in any way — instead, it's a convention that allows systems like [Sapper](https://sapper.svelte.technology) to delay rendering of a component until its data is ready.
|
|
||||||
|
|
||||||
See the section on [preloading](https://sapper.svelte.technology/guide#preloading) in the Sapper docs for more information.
|
|
Loading…
Reference in new issue