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 C Rules: 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

Component/instance options order

Component/instance options should be ordered consistently.

This is the default order we recommend for component options. They're split into categories, so you'll know where to add new properties from plugins.

  1. Global Awareness (requires knowledge beyond the component)

    • name
  2. Template Compiler Options (changes the way templates are compiled)

    • compilerOptions
  3. Template Dependencies (assets used in the template)

    • components
    • directives
  4. Composition (merges properties into the options)

    • extends
    • mixins
    • provide/inject
  5. Interface (the interface to the component)

    • inheritAttrs
    • props
    • emits
    • expose
  6. Composition API (the entry point for using the Composition API)

    • setup
  7. Local State (local reactive properties)

    • data
    • computed
  8. Events (callbacks triggered by reactive events)

    • watch
    • Lifecycle Events (in the order they are called)
      • beforeCreate
      • created
      • beforeMount
      • mounted
      • beforeUpdate
      • updated
      • activated
      • deactivated
      • beforeUnmount
      • unmounted
      • errorCaptured
      • renderTracked
      • renderTriggered
      • serverPrefetch (SSR only)
  9. Non-Reactive Properties (instance properties independent of the reactivity system)

    • methods
  10. Rendering (the declarative description of the component output)

    • template/render

Element attribute order

The attributes of elements (including components) should be ordered consistently.

This is the default order we recommend for component options. They're split into categories, so you'll know where to add custom attributes and directives.

  1. Definition (provides the component options)

    • is
  2. List Rendering (creates multiple variations of the same element)

    • v-for
  3. Conditionals (whether the element is rendered/shown)

    • v-if
    • v-else-if
    • v-else
    • v-show
    • v-cloak
  4. Render Modifiers (changes the way the element renders)

    • v-pre
    • v-once
  5. Global Awareness (requires knowledge beyond the component)

    • id
  6. Unique Attributes (attributes that require unique values)

    • ref
    • key
  7. Two-Way Binding (combining binding and events)

    • v-model
  8. Other Attributes (all unspecified bound & unbound attributes)

  9. Events (component event listeners)

    • v-on
  10. Content (overrides the content of the element)

    • v-html
    • v-text

Empty lines in component/instance options

You may want to add one empty line between multi-line properties, particularly if the options can no longer fit on your screen without scrolling.

When components begin to feel cramped or difficult to read, adding spaces between multi-line properties can make them easier to skim again. In some editors, such as Vim, formatting options like this can also make them easier to navigate with the keyboard.

选项式 API

✗ 反例

props: {
  value: {
    type: String,
    required: true
  },

  focused: {
    type: Boolean,
    default: false
  },

  label: String,
  icon: String
},

computed: {
  formattedValue() {
    // ...
  },

  inputClasses() {
    // ...
  }
}

✓ 正例

// No spaces are also fine, as long as the component
// is still easy to read and navigate.
props: {
  value: {
    type: String,
    required: true
  },
  focused: {
    type: Boolean,
    default: false
  },
  label: String,
  icon: String
},
computed: {
  formattedValue() {
    // ...
  },
  inputClasses() {
    // ...
  }
}

组合式 API

✗ 反例

defineProps({
  value: {
    type: String,
    required: true
  },
  focused: {
    type: Boolean,
    default: false
  },
  label: String,
  icon: String
})
const formattedValue = computed(() => {
  // ...
})
const inputClasses = computed(() => {
  // ...
})

✓ 正例

defineProps({
  value: {
    type: String,
    required: true
  },

  focused: {
    type: Boolean,
    default: false
  },

  label: String,
  icon: String
})

const formattedValue = computed(() => {
  // ...
})

const inputClasses = computed(() => {
  // ...
})

Single-file component top-level element order

Single-File Components should always order <script>, <template>, and <style> tags consistently, with <style> last, because at least one of the other two is always necessary.

✗ 反例

ComponentX.vue

<style>/* ... */</style>
<script>/* ... */</script>
<template>...</template>

ComponentA.vue

<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>

ComponentB.vue

<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>

✓ 正例

ComponentA.vue

<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>

ComponentB.vue

<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>

or

ComponentA.vue

<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>

ComponentB.vue

<template>...</template>
<script>/* ... */</script>
<style>/* ... */</style>
目录
    共 124 篇 Vue 3 官方文档(中文版) / C---推荐 0 字 0 行 cosolar
    创建 2026-11-08T09:54:58.889258289Z 更新 2026-11-08T09:57:31.985193812Z
    Logo

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

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

    订阅更新

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

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