// 父组件 ParentComponent.vue
<template>
<div>
<ChildComponent v-bind="$attrs" />
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent
},
inheritAttrs: false, // 防止属性被应用到根元素上
};
</script>
// 子组件 ChildComponent.vue
<template>
<div>
<p>Received attrs:</p>
<pre>{{ $attrs }}</pre>
</div>
</template>
<script>
export default {
props: {
// 这里可以定义一些明确的 prop,其他未定义的属性会通过 $attrs 传递
}
};
</script>
父组件 (ParentComponent.vue):
v-bind="$attrs" 将所有未在父组件中显式定义为 props 的属性传递给子组件。inheritAttrs: false 防止这些属性被应用到父组件的根元素上。子组件 (ChildComponent.vue):
$attrs 是一个对象,包含了所有从父组件传递过来但未在 props 中定义的属性。<pre>{{ $attrs }}</pre> 可以直观地展示接收到的属性。上一篇:vue清除缓存
下一篇:vue 关闭eslint
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站