Skip to content

Commit fa6e83a

Browse files
authored
Merge pull request #197 from sveltejs/gh-168
<:Component>
2 parents 3675999 + 5501d74 commit fa6e83a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

content/guide/06-special-components.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,81 @@ Sometimes, a component needs to embed itself recursively — for example if you
2525
```
2626

2727

28+
### <:Component> tags
29+
30+
If you don't know what kind of component to render until the app runs — in other words, it's driven by state — you can use `<:Component>`:
31+
32+
```html
33+
<input type=checkbox bind:checked=foo> foo
34+
<:Component {foo ? Red : Blue} name='thing'/>
35+
36+
<script>
37+
import Red from './Red.html';
38+
import Blue from './Blue.html';
39+
40+
export default {
41+
data() {
42+
return { Red, Blue }
43+
}
44+
};
45+
</script>
46+
```
47+
48+
```html-nested-Red
49+
<p style='color: red'>Red {{name}}</p>
50+
```
51+
52+
```html-nested-Blue
53+
<p style='color: blue'>Blue {{name}}</p>
54+
```
55+
56+
> Note that `Red` and `Blue` are items in `data`, *not* `components`, unlike if we were doing `<Red>` or `<Blue>`.
57+
58+
The expression inside the `{...}` can be any valid JavaScript expression. For example, it could be a [computed property](#computed-properties):
59+
60+
```html
61+
<label><input type=radio bind:group=size value='small'> small</label>
62+
<label><input type=radio bind:group=size value='medium'> medium</label>
63+
<label><input type=radio bind:group=size value='large'> large</label>
64+
65+
<:Component {Size}/>
66+
67+
<script>
68+
import Small from './Small.html';
69+
import Medium from './Medium.html';
70+
import Large from './Large.html';
71+
72+
export default {
73+
computed: {
74+
Size: size => {
75+
if (size === 'small') return Small;
76+
if (size === 'medium') return Medium;
77+
return Large;
78+
}
79+
}
80+
};
81+
</script>
82+
```
83+
84+
```html-nested-Small
85+
<p style='font-size: 12px'>small</p>
86+
```
87+
88+
```html-nested-Medium
89+
<p style='font-size: 18px'>medium</p>
90+
```
91+
92+
```html-nested-Large
93+
<p style='font-size: 32px'>LARGE</p>
94+
```
95+
96+
```hidden-data
97+
{
98+
"size": "medium"
99+
}
100+
```
101+
102+
28103
### <:Window> tags
29104

30105
The `<:Window>` tag gives you a convenient way to declaratively add event listeners to `window`. Event listeners are automatically removed when the component is destroyed.

0 commit comments

Comments
 (0)