diff --git a/src/style-guide/index.md b/src/style-guide/index.md deleted file mode 100644 index 8f7b747f6..000000000 --- a/src/style-guide/index.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -outline: deep ---- - -# Style Guide {#style-guide} - -::: warning Note -This Vue.js Style Guide is outdated and needs to be reviewed. If you have any questions or suggestions, please [open an issue](https://github.com/vuejs/docs/issues/new). -::: - -This is the official style guide for Vue-specific code. If you use Vue in a project, it's a great reference to avoid errors, bikeshedding, and anti-patterns. However, we don't believe that any style guide is ideal for all teams or projects, so mindful deviations are encouraged based on past experience, the surrounding tech stack, and personal values. - -For the most part, we also avoid suggestions about JavaScript or HTML in general. We don't mind whether you use semicolons or trailing commas. We don't mind whether your HTML uses single-quotes or double-quotes for attribute values. Some exceptions will exist however, where we've found that a particular pattern is helpful in the context of Vue. - -Finally, we've split rules into four categories: - -## Rule Categories {#rule-categories} - -### Priority A: Essential (Error Prevention) {#priority-a-essential-error-prevention} - -These rules help prevent errors, so learn and abide by them at all costs. Exceptions may exist, but should be very rare and only be made by those with expert knowledge of both JavaScript and Vue. - -- [See all priority A rules](./rules-essential) - -### Priority B: Strongly Recommended {#priority-b-strongly-recommended} - -These rules have been found to improve readability and/or developer experience in most projects. Your code will still run if you violate them, but violations should be rare and well-justified. - -- [See all priority B rules](./rules-strongly-recommended) - -### Priority C: Recommended {#priority-c-recommended} - -Where multiple, equally good options exist, an arbitrary choice can be made to ensure consistency. In these rules, we describe each acceptable option and suggest a default choice. That means you can feel free to make a different choice in your own codebase, as long as you're consistent and have a good reason. Please do have a good reason though! By adapting to the community standard, you will: - -1. Train your brain to more easily parse most of the community code you encounter -2. Be able to copy and paste most community code examples without modification -3. Often find new hires are already accustomed to your preferred coding style, at least in regards to Vue - -- [See all priority C rules](./rules-recommended) - -### Priority D: Use with Caution {#priority-d-use-with-caution} - -Some features of Vue exist to accommodate rare edge cases or smoother migrations from a legacy code base. When overused however, they can make your code more difficult to maintain or even become a source of bugs. These rules shine a light on potentially risky features, describing when and why they should be avoided. - -- [See all priority D rules](./rules-use-with-caution) diff --git a/src/style-guide/rules-essential.md b/src/style-guide/rules-essential.md deleted file mode 100644 index 0872dc499..000000000 --- a/src/style-guide/rules-essential.md +++ /dev/null @@ -1,426 +0,0 @@ -# Priority A Rules: Essential {#priority-a-rules-essential} - -::: warning Note -This Vue.js Style Guide is outdated and needs to be reviewed. If you have any questions or suggestions, please [open an issue](https://github.com/vuejs/docs/issues/new). -::: - -These rules help prevent errors, so learn and abide by them at all costs. Exceptions may exist, but should be very rare and only be made by those with expert knowledge of both JavaScript and Vue. - -## Use multi-word component names {#use-multi-word-component-names} - -User component names should always be multi-word, except for root `App` components. This [prevents conflicts](https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name) with existing and future HTML elements, since all HTML elements are a single word. - -
-

Bad

- -```vue-html - - - - - -``` - -
- -
-

Good

- -```vue-html - - - - - -``` - -
- -## Use detailed prop definitions {#use-detailed-prop-definitions} - -In committed code, prop definitions should always be as detailed as possible, specifying at least type(s). - -::: details Detailed Explanation -Detailed [prop definitions](/guide/components/props#prop-validation) have two advantages: - -- They document the API of the component, so that it's easy to see how the component is meant to be used. -- In development, Vue will warn you if a component is ever provided incorrectly formatted props, helping you catch potential sources of error. - ::: - -
- -
-

Bad

- -```js -// This is only OK when prototyping -props: ['status'] -``` - -
- -
-

Good

- -```js -props: { - status: String -} -``` - -```js -// Even better! -props: { - status: { - type: String, - required: true, - - validator: value => { - return [ - 'syncing', - 'synced', - 'version-conflict', - 'error' - ].includes(value) - } - } -} -``` - -
- -
- -
- -
-

Bad

- -```js -// This is only OK when prototyping -const props = defineProps(['status']) -``` - -
- -
-

Good

- -```js -const props = defineProps({ - status: String -}) -``` - -```js -// Even better! - -const props = defineProps({ - status: { - type: String, - required: true, - - validator: (value) => { - return ['syncing', 'synced', 'version-conflict', 'error'].includes( - value - ) - } - } -}) -``` - -
- -
- -## Use keyed `v-for` {#use-keyed-v-for} - -`key` with `v-for` is _always_ required on components, in order to maintain internal component state down the subtree. Even for elements though, it's a good practice to maintain predictable behavior, such as [object constancy](https://bost.ocks.org/mike/constancy/) in animations. - -::: details Detailed Explanation -Let's say you have a list of todos: - -
- -```js -data() { - return { - todos: [ - { - id: 1, - text: 'Learn to use v-for' - }, - { - id: 2, - text: 'Learn to use key' - } - ] - } -} -``` - -
- -
- -```js -const todos = ref([ - { - id: 1, - text: 'Learn to use v-for' - }, - { - id: 2, - text: 'Learn to use key' - } -]) -``` - -
- -Then you sort them alphabetically. When updating the DOM, Vue will optimize rendering to perform the cheapest DOM mutations possible. That might mean deleting the first todo element, then adding it again at the end of the list. - -The problem is, there are cases where it's important not to delete elements that will remain in the DOM. For example, you may want to use `` to animate list sorting, or maintain focus if the rendered element is an ``. In these cases, adding a unique key for each item (e.g. `:key="todo.id"`) will tell Vue how to behave more predictably. - -In our experience, it's better to _always_ add a unique key, so that you and your team simply never have to worry about these edge cases. Then in the rare, performance-critical scenarios where object constancy isn't necessary, you can make a conscious exception. -::: - -
-

Bad

- -```vue-html - -``` - -
- -
-

Good

- -```vue-html - -``` - -
- -## Avoid `v-if` with `v-for` {#avoid-v-if-with-v-for} - -**Never use `v-if` on the same element as `v-for`.** - -There are two common cases where this can be tempting: - -- To filter items in a list (e.g. `v-for="user in users" v-if="user.isActive"`). In these cases, replace `users` with a new computed property that returns your filtered list (e.g. `activeUsers`). - -- To avoid rendering a list if it should be hidden (e.g. `v-for="user in users" v-if="shouldShowUsers"`). In these cases, move the `v-if` to a container element (e.g. `ul`, `ol`). - -::: details Detailed Explanation -When Vue processes directives, `v-if` has a higher priority than `v-for`, so that this template: - -```vue-html - -``` - -Will throw an error, because the `v-if` directive will be evaluated first and the iteration variable `user` does not exist at this moment. - -This could be fixed by iterating over a computed property instead, like this: - -
- -```js -computed: { - activeUsers() { - return this.users.filter(user => user.isActive) - } -} -``` - -
- -
- -```js -const activeUsers = computed(() => { - return users.filter((user) => user.isActive) -}) -``` - -
- -```vue-html - -``` - -Alternatively, we can use a `