Logo
首页
前沿
AI热点新闻
36氦头条
博客
开源
极简简历
极简智库
瞬间
相册
友链
知识库
创作者
搜索文章 Ctrl K

Vue 3 官方文档(中文版)

返回 0
  • 导读
  • 开始
    • 简介
    • 快速上手
  • 基础
    • 创建一个应用
    • 模板语法
    • 响应式基础
    • 计算属性
    • 类与样式绑定
    • 条件渲染
    • 列表渲染
    • 事件处理
    • 表单输入绑定
    • 侦听器
    • 模板引用
    • 组件基础
    • 生命周期
  • 深入组件
    • 注册
    • Props
    • 事件
    • 组件-v-model
    • 透传-Attributes
    • 插槽
    • 依赖注入
    • 异步组件
  • 逻辑复用
    • 组合式函数
    • 自定义指令
    • 插件
  • 内置组件
    • Transition
    • TransitionGroup
    • KeepAlive
    • Teleport
    • Suspense
  • 应用规模化
    • 单文件组件
    • 工具链
    • 路由
    • 状态管理
    • 测试
    • 服务端渲染-SSR
  • 最佳实践
    • 生产部署
    • 性能优化
    • 无障碍访问
    • 安全
  • TypeScript
    • 总览
    • TS-与组合式-API
    • TS-与选项式-API
  • 进阶主题
    • 使用-Vue-的多种方式
    • 组合式-API-常见问答
    • 深入响应式系统
    • 渲染机制
    • 渲染函数-&-JSX
    • Vue-与-Web-Components
    • 动画技巧
  • API-参考
    • 全局-API
      • 应用实例
      • 通用
    • 组合式-API
      • setup
      • 响应式--核心
      • 响应式--工具
      • 响应式--进阶
      • 生命周期钩子
      • 依赖注入
      • 辅助
    • 选项式-API
      • 状态选项
      • 渲染选项
      • 生命周期选项
      • 组合选项
      • 其他杂项
      • 组件实例
    • 内置内容
      • 指令
      • 组件
      • 特殊元素
      • 特殊-Attributes
    • 单文件组件
      • 语法定义
      • script-setup
      • CSS-功能
    • 进阶-API
      • 自定义元素
      • 渲染函数
      • 服务端渲染
      • TypeScript-工具类型
      • 自定义渲染
      • 编译时标志
  • 互动教程
    • 第-1-步
    • 开始
    • 声明式渲染
    • Attribute-绑定
    • 事件监听
    • 表单绑定
    • 条件渲染
    • 列表渲染
    • 计算属性
    • 生命周期和模板引用
    • 侦听器
    • 组件
    • Props
    • Emits
    • 插槽
    • 你做到了!
  • 风格指南
    • 概览
    • A---必要的
    • B---强烈推荐
    • C---推荐
    • D---谨慎使用
  • 关于
    • 常见问题
    • 版本发布
    • 社区指南
    • 关于中文翻译

Priority A Rules: Essential

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

User component names should always be multi-word, except for root App components. This prevents conflicts with existing and future HTML elements, since all HTML elements are a single word.

✗ 反例

<!-- in pre-compiled templates -->
<Item />

<!-- in in-DOM templates -->
<item></item>

✓ 正例

<!-- in pre-compiled templates -->
<TodoItem />

<!-- in in-DOM templates -->
<todo-item></todo-item>

Use detailed prop definitions

In committed code, prop definitions should always be as detailed as possible, specifying at least type(s).

[!NOTE] Detailed Explanation

Detailed prop definitions 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.

选项式 API

✗ 反例

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

✓ 正例

props: {
  status: String
}
// Even better!
props: {
  status: {
    type: String,
    required: true,

    validator: value => {
      return [
        'syncing',
        'synced',
        'version-conflict',
        'error'
      ].includes(value)
    }
  }
}

组合式 API

✗ 反例

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

✓ 正例

const props = defineProps({
  status: String
})
// Even better!

const props = defineProps({
  status: {
    type: String,
    required: true,

    validator: (value) => {
      return ['syncing', 'synced', 'version-conflict', 'error'].includes(
        value
      )
    }
  }
})

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 in animations.

说明:Detailed Explanation

Let's say you have a list of todos:

选项式 API

data() {
  return {
    todos: [
      {
        id: 1,
        text: 'Learn to use v-for'
      },
      {
        id: 2,
        text: 'Learn to use key'
      }
    ]
  }
}

组合式 API

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 <transition-group> to animate list sorting, or maintain focus if the rendered element is an <input>. 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.

✗ 反例

<ul>
  <li v-for="todo in todos">
    {{ todo.text }}
  </li>
</ul>

✓ 正例

<ul>
  <li
    v-for="todo in todos"
    :key="todo.id"
  >
    {{ todo.text }}
  </li>
</ul>

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

说明:Detailed Explanation

When Vue processes directives, v-if has a higher priority than v-for, so that this template:

<ul>
  <li
    v-for="user in users"
    v-if="user.isActive"
    :key="user.id"
  >
    {{ user.name }}
  </li>
</ul>

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:

选项式 API

computed: {
  activeUsers() {
    return this.users.filter(user => user.isActive)
  }
}

组合式 API

const activeUsers = computed(() => {
  return users.filter((user) => user.isActive)
})
<ul>
  <li
    v-for="user in activeUsers"
    :key="user.id"
  >
    {{ user.name }}
  </li>
</ul>

Alternatively, we can use a <template> tag with v-for to wrap the <li> element:

<ul>
  <template v-for="user in users" :key="user.id">
    <li v-if="user.isActive">
      {{ user.name }}
    </li>
  </template>
</ul>

✗ 反例

<ul>
  <li
    v-for="user in users"
    v-if="user.isActive"
    :key="user.id"
  >
    {{ user.name }}
  </li>
</ul>

✓ 正例

<ul>
  <li
    v-for="user in activeUsers"
    :key="user.id"
  >
    {{ user.name }}
  </li>
</ul>
<ul>
  <template v-for="user in users" :key="user.id">
    <li v-if="user.isActive">
      {{ user.name }}
    </li>
  </template>
</ul>

Use component-scoped styling

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. It does not require that the scoped attribute be used. Scoping could be through CSS modules, a class-based strategy such as BEM, or another library/convention.

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

This makes overriding internal styles easier, with human-readable class names that don't have too high specificity, but are still very unlikely to result in a conflict.

[!NOTE] Detailed Explanation

If you are developing a large project, working with other developers, or sometimes include 3rd-party HTML/CSS (e.g. from Auth0), consistent scoping will ensure that your styles only apply to the components they are meant for.

Beyond the scoped attribute, using unique class names can help ensure that 3rd-party CSS does not apply to your own HTML. For example, many projects use the button, btn, or icon class names, so even if not using a strategy such as BEM, adding an app-specific and/or component-specific prefix (e.g. ButtonClose-icon) can provide some protection.

✗ 反例

<template>
  <button class="btn btn-close">×</button>
</template>

<style>
.btn-close {
  background-color: red;
}
</style>

✓ 正例

<template>
  <button class="button button-close">×</button>
</template>

<!-- Using the `scoped` attribute -->
<style scoped>
.button {
  border: none;
  border-radius: 2px;
}

.button-close {
  background-color: red;
}
</style>
<template>
  <button :class="[$style.button, $style.buttonClose]">×</button>
</template>

<!-- Using CSS modules -->
<style module>
.button {
  border: none;
  border-radius: 2px;
}

.buttonClose {
  background-color: red;
}
</style>
<template>
  <button class="c-Button c-Button--close">×</button>
</template>

<!-- Using the BEM convention -->
<style>
.c-Button {
  border: none;
  border-radius: 2px;
}

.c-Button--close {
  background-color: red;
}
</style>
目录
    共 124 篇 Vue 3 官方文档(中文版) / A---必要的 0 字 0 行 cosolar
    创建 2026-11-08T09:54:58.775928547Z 更新 2026-11-08T09:57:30.655645322Z
    Logo

    以极简为笔,以求索为墨。 简单书写,深度思考,每一次记录,都是与内心的对话。

    30,291 访问量
    219 点赞数
    0 评论数
    162 文章数 13 分类数 243 标签数
    2026-06-06 建站时间
    中国·北京 博主地址

    订阅更新

    订阅我们的技术文章,每周精选内容送达你的邮箱

    隐私政策 | 使用条款 | 友情链接 | 关于我们
    本主题由 腾讯云 提供云服务
    © 极简笔记 版权所有 津ICP备2026156702号-1
    Powered by Halo Theme halo-theme-cosolar