tweak docs a bit

pull/3096/head
Rich Harris 6 years ago
parent e09014b816
commit 6e6a675c67

@ -28,41 +28,60 @@ A `<script>` block contains JavaScript that runs when a component instance is cr
--- ---
Svelte uses the `export` keyword to mark a variable declaration as a *property* or *prop*, which means it becomes accessible to consumers of the component: Svelte uses the `export` keyword to mark a variable declaration as a *property* or *prop*, which means it becomes accessible to consumers of the component (see the section on [attributes and props](docs#Attributes_and_props) for more information).
```html ```html
<script> <script>
// these properties can be set externally
export let foo; export let foo;
// Values that are passed in as props
// are immediately available
console.log({ foo });
</script>
```
---
You can specify a default value, which will be used if the component's consumer doesn't specify a prop.
In development mode (see the [compiler options](docs#svelte_compile)), a warning will be printed if no default is provided and the consumer does not specify a value. To squelch this warning, ensure that a default is specified, even if it is `undefined`.
```html
<script>
export let bar = 'optional default value'; export let bar = 'optional default value';
export let baz = undefined;
</script>
```
---
// you can use export { ... as ... } to have If you export a `const`, `class` or `function`, it is readonly from outside the component. Function *expressions* are valid props, however.
// props whose names are reserved keywords
let clazz;
export { clazz as class };
// this property is readonly externally ```html
export const buzz = 'buzz'; <script>
// these are readonly
export const thisIs = 'readonly';
// Values that are passed in as props export function greet(name) {
// are immediately available alert(`hello ${name}!`);
console.log(foo, bar);
// Function expressions can also be props
export let format = (number) => (number.toFixed(2));
// Function declarations are added as methods
// on the component, rather than props
export function greetMethod() {
alert(`I'm a <${this.constructor.name}>!`);
} }
// you can also use export { ... as ... } to have // this is a prop
// methods whose names are reserved keywords export let format = n => n.toFixed(2);
function del() { </script>
do_something(); ```
}
export { del as delete }; ---
You can use reserved words as prop names.
```html
<script>
let className;
// creates a `class` property, even
// though it is a reserved word
export { className as class };
</script> </script>
``` ```
@ -81,8 +100,8 @@ Because Svelte's reactivity is based on assignments, using array methods like `.
let count = 0; let count = 0;
function handleClick () { function handleClick () {
// calling this function will trigger a re-render // calling this function will trigger an
// if the markup references `count` // update if the markup references `count`
count = count + 1; count = count + 1;
} }
</script> </script>

@ -20,7 +20,7 @@ A lowercase tag, like `<div>`, denotes a regular HTML element. A capitalised tag
``` ```
### Attributes ### Attributes and props
--- ---
@ -76,6 +76,16 @@ When the attribute name and value match (`name={name}`), they can be replaced wi
--- ---
By convention, values passed to components are referred to as *properties* or *props* rather than *attributes*, which are a feature of the DOM.
As with elements, `name={name}` can be replaced with the `{name}` shorthand.
```html
<Widget foo={bar} answer={42} text="hello"/>
```
---
*Spread attributes* allow many attributes or properties to be passed to an element or component at once. *Spread attributes* allow many attributes or properties to be passed to an element or component at once.
An element or component can have multiple spread attributes, interspersed with regular ones. An element or component can have multiple spread attributes, interspersed with regular ones.

Loading…
Cancel
Save