Skip to content

Commit c76ef68

Browse files
committed
custom properties
1 parent 8537c3e commit c76ef68

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- [Styles](#styles)
2020
- [Template Binding](#template-binding)
2121
- [Child Components](#child-components)
22+
- [Custom Properties](#custom-properties)
2223

2324
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2425

@@ -310,3 +311,20 @@ Then using the custom elements in the template:
310311
```
311312

312313
Also pull out all the cards to a Category component.
314+
315+
### Custom Properties
316+
317+
In previous module, we created static child components. Want to create dynamic child components, pass in data to them.
318+
One way to do this is with *custom properties*. Example [Post.vue](src/theme/Post.vue).
319+
320+
Use `props` property. This is an array that binds the property of the component with attribute of the element.
321+
322+
![bind-props](course-images/bind-props.png)
323+
324+
Move card snippet to Post component. Now it needs data passed in to it. This is done by binding the custom `<app-post>` element with the `post` json data, via `props`:
325+
326+
```html
327+
<div class="column is-one-third" v-for="(post, title) in posts" v-bind:key="post.id">
328+
<app-post :post="post"></app-post>
329+
</div>
330+
```

course-images/bind-props.png

139 KB
Loading

src/theme/Category.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
<template>
22
<div class="columns">
33
<div class="column is-one-third" v-for="(post, title) in posts" v-bind:key="post.id">
4-
<div class="card">
5-
<div class="card-content">
6-
<h3>{{ post.title }}</h3>
7-
{{ post.content }}
8-
</div>
9-
<footer class="card-footer">
10-
<a class="card-footer-item" :href="post.link" target="_blank">Read More</a>
11-
</footer>
12-
</div>
4+
<app-post :post="post"></app-post>
135
</div>
146
</div>
157
</template>
168

179
<script>
10+
import Post from './Post.vue'
1811
// create a view instance
1912
export default {
13+
components: {
14+
'app-post': Post
15+
},
2016
data () {
2117
return {
2218
posts: [

src/theme/Post.vue

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<template>
2+
<div class="card">
3+
<div class="card-content">
4+
<h3>{{ post.title }}</h3>
5+
{{ post.content }}
6+
</div>
7+
<footer class="card-footer">
8+
<a class="card-footer-item" :href="post.link" target="_blank">Read More</a>
9+
</footer>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
props: ['post']
16+
}
17+
</script>

0 commit comments

Comments
 (0)