Extraneous children found when component already has explicitly named default slot. These children will be ignored.vue(39)
时间: 2024-04-12 20:27:04 浏览: 171
vue3.X+element+cli4.0
The warning message "Extraneous children found when component already has explicitly named default slot. These children will be ignored." is usually encountered in Vue.js when you try to pass children to a component that already has an explicitly named default slot.
In Vue.js, a component can have multiple slots to render different content. By default, any content that is not explicitly assigned to a named slot is rendered in the default slot. However, if a component already has a named default slot, any additional children passed to the component will be ignored and produce this warning.
To resolve this issue, you have a few options:
1. If you intended to pass the children to the default slot, make sure that the component does not have any explicitly named default slot. Remove any `slot` attribute with a specific name from the component's template.
2. If you want to assign the children to a named slot, make sure to update the component's usage accordingly. Pass the children as a separate prop or wrap them in a container element and assign that container to the named slot.
Here's an example to illustrate the second option:
```vue
<template>
<div>
<slot name="header"></slot>
<slot></slot>
</div>
</template>
<script>
export default {
name: 'MyComponent',
}
</script>
```
In your parent component:
```vue
<template>
<my-component>
<template v-slot:header>
<!-- header content goes here -->
</template>
<!-- other content goes here -->
</my-component>
</template>
<script>
import MyComponent from '@/components/MyComponent.vue'
export default {
components: {
MyComponent,
},
}
</script>
```
By assigning the children to the named slot, you can avoid the warning message and ensure that the content is rendered correctly within the component.
阅读全文