Vue 3 官方文档(中文版)
返回
0
Props
子组件可以通过 props 从父组件接受动态数据。首先,需要声明它所接受的 props:
组合式 API
ChildComp.vue
<script setup>
const props = defineProps({
msg: String
})
</script>
注意 defineProps() 是一个编译时宏,并不需要导入。一旦声明,msg prop 就可以在子组件的模板中使用。它也可以通过 defineProps() 所返回的对象在 JavaScript 中访问。
// 在子组件中
export default {
props: {
msg: String
},
setup(props) {
// 访问 props.msg
}
}
一旦声明,msg prop 就会暴露在 this 上,并可以在子组件的模板中使用。接收到的 props 会作为第一个参数传递给 setup()。
选项式 API
// 在子组件中
export default {
props: {
msg: String
}
}
一旦声明,msg prop 就会暴露在 this 上,并可以在子组件的模板中使用。
父组件可以像声明 HTML attributes 一样传递 props。若要传递动态值,也可以使用 v-bind 语法:
<ChildComp :msg="greeting" />
<child-comp :msg="greeting"></child-comp>
现在在编辑器中自己尝试一下吧。
共 124 篇
Vue 3 官方文档(中文版)
/
Props
0 字
0 行
cosolar
创建
2026-11-08T09:54:58.697953767Z
更新
2026-11-08T09:57:33.911215981Z