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

Element selectors with scoped

Element selectors should be avoided with scoped.

Prefer class selectors over element selectors in scoped styles, because large numbers of element selectors are slow.

[!NOTE] Detailed Explanation

To scope styles, Vue adds a unique attribute to component elements, such as data-v-f3f3eg9. Then selectors are modified so that only matching elements with this attribute are selected (e.g. button[data-v-f3f3eg9]).

The problem is that large numbers of element-attribute selectors (e.g. button[data-v-f3f3eg9]) will be considerably slower than class-attribute selectors (e.g. .btn-close[data-v-f3f3eg9]), so class selectors should be preferred whenever possible.

✗ 反例

<template>
  <button>×</button>
</template>

<style scoped>
button {
  background-color: red;
}
</style>

✓ 正例

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

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

Implicit parent-child communication

Props and events should be preferred for parent-child component communication, instead of this.$parent or mutating props.

An ideal Vue application is props down, events up. Sticking to this convention makes your components much easier to understand. However, there are edge cases where prop mutation or this.$parent can simplify two components that are already deeply coupled.

The problem is, there are also many simple cases where these patterns may offer convenience. Beware: do not be seduced into trading simplicity (being able to understand the flow of your state) for short-term convenience (writing less code).

选项式 API

✗ 反例

app.component('TodoItem', {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },

  template: '<input v-model="todo.text">'
})
app.component('TodoItem', {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },

  methods: {
    removeTodo() {
      this.$parent.todos = this.$parent.todos.filter(
        (todo) => todo.id !== vm.todo.id
      )
    }
  },

  template: `
    <span>
      {{ todo.text }}
      <button @click="removeTodo">
        ×
      </button>
    </span>
  `
})

✓ 正例

app.component('TodoItem', {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },

  emits: ['input'],

  template: `
    <input
      :value="todo.text"
      @input="$emit('input', $event.target.value)"
    >
  `
})
app.component('TodoItem', {
  props: {
    todo: {
      type: Object,
      required: true
    }
  },

  emits: ['delete'],

  template: `
    <span>
      {{ todo.text }}
      <button @click="$emit('delete')">
        ×
      </button>
    </span>
  `
})

组合式 API

✗ 反例

<script setup>
defineProps({
  todo: {
    type: Object,
    required: true
  }
})
</script>

<template>
  <input v-model="todo.text" />
</template>
<script setup>
const props = defineProps({
  todo: {
    type: Object,
    required: true
  }
})

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="renameTodo">rename</button>
  </span>
</template>

✓ 正例

<script setup>
defineProps({
  todo: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['input'])
</script>

<template>
  <input :value="todo.text" @input="emit('input', $event.target.value)" />
</template>
<script setup>
const props = defineProps({
  todo: {
    type: Object,
    required: true
  }
})

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="renameTodo">rename</button>
  </span>
</template>
目录
    共 124 篇 Vue 3 官方文档(中文版) / D---谨慎使用 0 字 0 行 cosolar
    创建 2026-11-08T09:54:58.857927791Z 更新 2026-11-08T09:57:32.699400098Z
    Logo

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

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

    订阅更新

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

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