data, methods 结构固定,新手容易理解 this。setup 是 Composition API 的入口。
const { title } = props 会让 title 失去响应式。const { title } = toRefs(props)const title = toRef(props, 'title')attrs: 非 Prop 属性(class, style)。slots: 插槽内容。emit: 触发事件的方法。expose: 暴露公共属性(Vue 3.2+)。<script setup> 语法糖这是 Vue 3 推荐的写法,更简洁,无需 return。
const props = defineProps(['foo'])const emit = defineEmits(['change'])useSlots()useAttrs()defineExpose({ ... })Q: 为什么 setup 里没有 this?
setup 执行时机在 beforeCreate 之前,组件实例还没完全创建好。
Vue 刻意屏蔽 this,为了防止开发者在 Composition API 中混用 Options API 的思维,强制使用参数传递或 Hook 获取。