Skip to content

Commit b02eb66

Browse files
authored
Add "destructuring in templates" section (#54)
]This commit adds a new section documenting our team discussion about avoiding destructuring in HTML templates.
1 parent 87936a4 commit b02eb66

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

javascript/vue/README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
# Vue Guide
22

3-
> TODO: add style guide
4-
5-
Until then please refer to these guides:
3+
This styleguide is a work-in-progress, documenting how Cloud Four prefers to write Vue. As we continue to expand it, please refer to these guides for additional best practices:
64

75
* [Official Vue Style Guide](https://vuejs.org/v2/style-guide/)
86
* [Deverus Vue Style Guide](https://gist.github.com/brianboyko/91fdfb492071e743e389d84eee002342)
97
* [Erik reviews the Deverus Style Guide](https://www.youtube.com/watch?v=38XnZ3EJqYQ)
8+
9+
## Destructuring in Templates
10+
11+
Try to avoid destructuring in HTML templates, to make it obvious where a particular variable is coming from. Since HTML templates have access to props and local variables in `v-for` loops, clarity is favored over brevity in this case.
12+
13+
### Avoid This
14+
15+
Notice that in this example with nested `v-for` loops, destructuring makes it unclear where certain variables are coming from, and some repeated variables like `title` need to be remapped.
16+
17+
```vue
18+
<section v-for="{ title, id, subcategories, items } in FAQs" :key="id">
19+
<h2>{{ title }}</h2>
20+
21+
<section
22+
v-for="{ title: subtitle, items: subItems, subcategoryId } in subcategories"
23+
:key="subcategoryId"
24+
>
25+
<h3>{{ subtitle }}</h3>
26+
27+
<details v-for="{ question, answer, itemId } in subItems" :key="itemId">
28+
<summary>{{ question }}</summary>
29+
<div>{{ answer }}</div>
30+
</details>
31+
</section>
32+
33+
<details v-for="{ question, answer, itemId } in items" :key="itemId">
34+
<summary>{{ question }}</summary>
35+
<div>{{ answer }}</div>
36+
</details>
37+
</section>
38+
```
39+
40+
### Prefer This
41+
42+
By not destructuring, the nested `v-for` loops are much easier to understand.
43+
44+
```vue
45+
<section v-for="category in FAQs" :key="category.id">
46+
<h2>{{ category.title }}</h2>
47+
48+
<section v-for="subcategory in category.subcategories" :key="subcategory.id">
49+
<h3>{{ subcategory.title }}</h3>
50+
51+
<details v-for="item in subcategory.items" :key="item.id">
52+
<summary>{{ item.question }}</summary>
53+
<div>{{ item.answer }}</div>
54+
</details>
55+
</section>
56+
57+
<details v-for="item in category.items" :key="item.id">
58+
<summary>{{ item.question }}</summary>
59+
<div>{{ item.answer }}</div>
60+
</details>
61+
</section>
62+
```
63+
64+
Note that while these examples with nested `v-for` loops are extreme, they highlight a possible problem with destructuring in HTML templates. As a team, we decided to favor consistency, and rather than have a rule like "always destructure except in loops," we've standardized on "avoid destructuring in templates."

0 commit comments

Comments
 (0)