@@ -25,6 +25,81 @@ Sometimes, a component needs to embed itself recursively — for example if you
25
25
```
26
26
27
27
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
+
28
103
### <: Window > tags
29
104
30
105
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