Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/style-guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ 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.
Expand Down
6 changes: 1 addition & 5 deletions src/style-guide/rules-essential.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# 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}
Expand Down Expand Up @@ -337,7 +333,7 @@ Alternatively, we can use a `<template>` tag with `v-for` to wrap the `<li>` ele

For applications, styles in a top-level `App` component and in layout components may be global, but all other components should always be scoped.

This is only relevant for [Single-File Components](/guide/scaling-up/sfc). It does _not_ require that the [`scoped` attribute](https://vue-loader.vuejs.org/guide/scoped-css.html) be used. Scoping could be through [CSS modules](https://vue-loader.vuejs.org/guide/css-modules.html), a class-based strategy such as [BEM](http://getbem.com/), or another library/convention.
This is only relevant for [Single-File Components](/guide/scaling-up/sfc). It does _not_ require that the [`scoped` attribute](/api/sfc-css-features#scoped-css) be used. Scoping could be through [CSS modules](/api/sfc-css-features#css-modules), a class-based strategy such as [BEM](https://getbem.com/), or another library/convention.

**Component libraries, however, should prefer a class-based strategy instead of using the `scoped` attribute.**

Expand Down
6 changes: 2 additions & 4 deletions src/style-guide/rules-recommended.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Priority C Rules: Recommended {#priority-c-rules-recommended}

::: 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).
:::

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
Expand Down Expand Up @@ -40,6 +36,7 @@ This is the default order we recommend for component options. They're split into
- `inheritAttrs`
- `props`
- `emits`
- `expose`

6. **Composition API** (the entry point for using the Composition API)

Expand Down Expand Up @@ -67,6 +64,7 @@ This is the default order we recommend for component options. They're split into
- `errorCaptured`
- `renderTracked`
- `renderTriggered`
- `serverPrefetch` (SSR only)

9. **Non-Reactive Properties** (instance properties independent of the reactivity system)

Expand Down
28 changes: 8 additions & 20 deletions src/style-guide/rules-strongly-recommended.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Priority B Rules: Strongly Recommended {#priority-b-rules-strongly-recommended}

::: 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 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.

## Component files {#component-files}
Expand Down Expand Up @@ -101,23 +97,15 @@ Some advantages of this convention:

- Since component names should always be multi-word, this convention prevents you from having to choose an arbitrary prefix for simple component wrappers (e.g. `MyButton`, `VueButton`).

- Since these components are so frequently used, you may want to simply make them global instead of importing them everywhere. A prefix makes this possible with Webpack:
- Since these components are so frequently used, you may want to simply make them global instead of importing them everywhere. A prefix makes this possible with Vite:

```js
const requireComponent = require.context(
'./src',
true,
/Base[A-Z]\w+\.(vue|js)$/
)
requireComponent.keys().forEach(function (fileName) {
let baseComponentConfig = requireComponent(fileName)
baseComponentConfig =
baseComponentConfig.default || baseComponentConfig
const baseComponentName =
baseComponentConfig.name ||
fileName.replace(/^.+\//, '').replace(/\.\w+$/, '')
app.component(baseComponentName, baseComponentConfig)
})
const modules = import.meta.glob('./src/**/Base*.vue', { eager: true })
for (const path in modules) {
const config = modules[path].default
const name = config.name || path.match(/Base[A-Z]\w+/)[0]
app.component(name, config)
}
```

:::
Expand Down Expand Up @@ -317,7 +305,7 @@ components/

Components that self-close communicate that they not only have no content, but are **meant** to have no content. It's the difference between a blank page in a book and one labeled "This page intentionally left blank." Your code is also cleaner without the unnecessary closing tag.

Unfortunately, HTML doesn't allow custom elements to be self-closing - only [official "void" elements](https://www.w3.org/TR/html/syntax.html#void-elements). That's why the strategy is only possible when Vue's template compiler can reach the template before the DOM, then serve the DOM spec-compliant HTML.
Unfortunately, HTML doesn't allow custom elements to be self-closing - only [official "void" elements](https://html.spec.whatwg.org/multipage/syntax.html#void-elements). That's why the strategy is only possible when Vue's template compiler can reach the template before the DOM, then serve the DOM spec-compliant HTML.

<div class="style-example style-example-bad">
<h3>Bad</h3>
Expand Down
32 changes: 13 additions & 19 deletions src/style-guide/rules-use-with-caution.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Priority D Rules: Use with Caution {#priority-d-rules-use-with-caution}

::: 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).
:::

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.

## Element selectors with `scoped` {#element-selectors-with-scoped}
Expand Down Expand Up @@ -179,31 +175,24 @@ defineProps({

```vue
<script setup>
import { getCurrentInstance } from 'vue'

const props = defineProps({
todo: {
type: Object,
required: true
}
})

const instance = getCurrentInstance()

function removeTodo() {
const parent = instance.parent
if (!parent) return

parent.props.todos = parent.props.todos.filter((todo) => {
return todo.id !== props.todo.id
})
function renameTodo() {
// Mutates the parent's reactive object via the prop
// In other words, the child is reaching into and changing parent-owned state.
props.todo.text = 'renamed by child'
}
</script>

<template>
<span>
{{ todo.text }}
<button @click="removeTodo">×</button>
<button @click="renameTodo">rename</button>
</span>
</template>
```
Expand Down Expand Up @@ -232,20 +221,25 @@ const emit = defineEmits(['input'])

```vue
<script setup>
defineProps({
const props = defineProps({
todo: {
type: Object,
required: true
}
})

const emit = defineEmits(['delete'])
const emit = defineEmits(['update:todo'])

function renameTodo() {
// Emit a new object — the parent owns the update.
emit('update:todo', { ...props.todo, text: 'renamed by parent' })
}
</script>

<template>
<span>
{{ todo.text }}
<button @click="emit('delete')">×</button>
<button @click="renameTodo">rename</button>
</span>
</template>
```
Expand Down