|
1 | 1 | # Vue Guide
|
2 | 2 |
|
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: |
6 | 4 |
|
7 | 5 | * [Official Vue Style Guide](https://vuejs.org/v2/style-guide/)
|
8 | 6 | * [Deverus Vue Style Guide](https://gist.github.com/brianboyko/91fdfb492071e743e389d84eee002342)
|
9 | 7 | * [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