Vue 3 setup() 函数接收 props 属性 - 如何使用 selectedData
在 Vue 3 的 setup() 函数中,可以使用 props 来接收父组件传递的数据。你可以像下面这样来接收 selectedData 属性:
import { ref } from 'vue';
export default {
props: ['selectedData'],
setup(props) {
// 使用 ref() 将 props.selectedData 包装成响应式对象
const selectedData = ref(props.selectedData);
// 在这里可以使用 selectedData
return {
selectedData
};
}
}
在 setup() 函数中,我们通过参数 props 来获取父组件传递的属性。然后使用 ref() 将 props.selectedData 包装成响应式对象,并将其返回。
现在,你就可以在组件中使用 selectedData 了,并且当父组件传递的 selectedData 发生变化时,组件也会相应地更新。
原文地址: https://www.cveoy.top/t/topic/qDIl 著作权归作者所有。请勿转载和采集!