< menuitems />缺少父母<菜单 />组件错误(插槽中的菜单)

发布于 2025-02-11 21:13:22 字数 2453 浏览 1 评论 0 原文

我正在Vue3中创建一个at< 下拉>>的组件。

下拉组件依次使用其他组件:菜单,menuitems,menuitem和menubutton headelessui/vue库

我的想法是,您可以将任何内容放在下拉>中,这就是为什么我创建了一个称为 #contentdropdown 的插槽的原因。

问题是,当我将此插槽内容传递给下拉列表组件时,VUE给我以下错误:

< menuitems />缺少父母<菜单 />组件

这是我的下拉组件代码:

<template>
  <Menu as="div" class="relative inline-block text-left">
    <div>
      <MenuButton class="btn inline-flex justify-center w-full text-sm" :class="'btn-'+variant">
        Options
        <ChevronDownIcon class="-mr-1 ml-2 h-5 w-5" aria-hidden="true" />
      </MenuButton>
    </div>

    <transition enter-active-class="transition ease-out duration-100" enter-from-class="transform opacity-0 scale-95" enter-to-class="transform opacity-100 scale-100" leave-active-class="transition ease-in duration-75" leave-from-class="transform opacity-100 scale-100" leave-to-class="transform opacity-0 scale-95">
      <slot name="contentdropdown"></slot>
    </transition>
  </Menu>
</template>

<script>
import { Menu, MenuButton } from '@headlessui/vue'
import { ChevronDownIcon } from '@heroicons/vue/solid'
import { vVariantProp } from '../../../../constants'
import { reactive, computed } from 'vue';

export default {

    name: 'dropdown',

    props: {
  
        ...vVariantProp,
    },

    setup(props) {

        props = reactive(props);
    
        return {
           
        }
    },
    
};
</script>

为什么需要parent组件称为菜单?

这就是我通过其#ContentDropdown插槽传递到内容的下拉组件的方式:

<Dropdown v-bind="{'variant':'primary'}">
          <template #contentdropdown>
            <MenuItems class="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 focus:outline-none">
              <div class="py-1">
                <MenuItem>
                  <a href="#" class="block px-4 py-2 text-sm">Subitem1</a>
                </MenuItem>
                <MenuItem>
                  <a href="#" class="block px-4 py-2 text-sm">Subitem2</a>
                </MenuItem>
              </div>
            </MenuItems>
          </template>
        </Dropdown>

I am creating a component in Vue3 called <Dropdown>.

The Dropdown component uses other components in turn: Menu, MenuItems, MenuItem and MenuButton from the headlessui/vue library.

My idea is that you can put any content in the Dropdown, that's why I created a slot called #contentdropdown.

The problem is that when I pass this slot content to the Dropdown component, Vue gives me the following error:

< MenuItems /> is missing a parent < Menu /> component

This is my Dropdown componente code:

<template>
  <Menu as="div" class="relative inline-block text-left">
    <div>
      <MenuButton class="btn inline-flex justify-center w-full text-sm" :class="'btn-'+variant">
        Options
        <ChevronDownIcon class="-mr-1 ml-2 h-5 w-5" aria-hidden="true" />
      </MenuButton>
    </div>

    <transition enter-active-class="transition ease-out duration-100" enter-from-class="transform opacity-0 scale-95" enter-to-class="transform opacity-100 scale-100" leave-active-class="transition ease-in duration-75" leave-from-class="transform opacity-100 scale-100" leave-to-class="transform opacity-0 scale-95">
      <slot name="contentdropdown"></slot>
    </transition>
  </Menu>
</template>

<script>
import { Menu, MenuButton } from '@headlessui/vue'
import { ChevronDownIcon } from '@heroicons/vue/solid'
import { vVariantProp } from '../../../../constants'
import { reactive, computed } from 'vue';

export default {

    name: 'dropdown',

    props: {
  
        ...vVariantProp,
    },

    setup(props) {

        props = reactive(props);
    
        return {
           
        }
    },
    
};
</script>

Why does it need the parent component called Menu?, if in fact I am already painting the slot inside the component and also importing it inside the Dropdown component.

This is how I pass to the Dropdown component the content through its #contentdropdown slot:

<Dropdown v-bind="{'variant':'primary'}">
          <template #contentdropdown>
            <MenuItems class="origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 focus:outline-none">
              <div class="py-1">
                <MenuItem>
                  <a href="#" class="block px-4 py-2 text-sm">Subitem1</a>
                </MenuItem>
                <MenuItem>
                  <a href="#" class="block px-4 py-2 text-sm">Subitem2</a>
                </MenuItem>
              </div>
            </MenuItems>
          </template>
        </Dropdown>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏别ゝ 2025-02-18 21:13:22

错误&lt; menuitems /&gt;缺少父母&lt;菜单 /&gt;组件不是VUE特定错误。这是 headlessui/vue - source

menuitems component(以及 menubuton etceet et et eft-请参阅 doc )被设计为在菜单 component中使用。它正在使用注入来点击状态提供了通过菜单组件。您无能为力 - 它是这样设计的

问题是,在vue中始终在&lt; template#contentDropdown&gt; )始终呈现=“ https://vuejs.org/guide/components/slots.html#render-scope” rel =“ nofollow noreferrer”> parent scope

父模板中的所有内容都在父范围内编译;子模板中的所有内容都在子范围中编译。

这意味着 menuitems 作为插槽内容渲染的无法访问data 提供菜单组件组件呈现在您的下拉code 组件

我看不出任何方法可以克服此限制。您需要更改设计(或将用例描述为 Headlessui/vue 维护者,并要求他们实现替代方法来共享

The error <MenuItems /> is missing a parent <Menu /> component is not a Vue specific error. It is an error thrown by headlessui/vue - source

MenuItems component (as well as MenuButon etc - see doc) is designed to be used inside Menu component. It is using inject to tap into state provideded by the Menu component. There is nothing you can do about it - it is designed that way

Problem is that slot content (everything inside <template #contentdropdown> in the last code example) in Vue is always rendered in parent scope

Everything in the parent template is compiled in parent scope; everything in the child template is compiled in the child scope.

This means that MenuItems rendered as slot content has no access to data provideded by the Menu component rendered inside your Dropdown component

I don't see any way to overcome this limitation. You'll need to change your design (or describe your use case to headlessui/vue maintainers and ask them to implement alternative approach to share MenuContext with child components - for example using slot props)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文