英文:
Can I use empty <></> tags in vue
问题
当一个组件有多个子元素或组件时,它们应该嵌套在单个父标签`<div></div>`中,但这可能不是我想要的。在这种情况下,React允许使用空的`<></>`标签,例如:
我的问题是,在Vue中是否也可以使用这种方法?我似乎在文档中找不到任何相关信息。如果这不可能,是否有解决方法?
英文:
Where a component has multiple child elements or components, they are supposed to be nested within a single parent tag <div></div>
for example, but this may not be what I want, React allows the use of empty <></>
tags in this case, here is an instance:
function Example() {
return (
<>
<RadioGroup defaultValue={placement} onChange={setPlacement}>
<Stack direction='row' mb='4'>
<Radio value='top'>Top</Radio>
<Radio value='right'>Right</Radio>
<Radio value='bottom'>Bottom</Radio>
<Radio value='left'>Left</Radio>
</Stack>
</RadioGroup>
<RadioGroup defaultValue={placement} onChange={setPlacement}>
<Stack direction='row' mb='4'>
<Radio value='top'>Top</Radio>
<Radio value='right'>Right</Radio>
<Radio value='bottom'>Bottom</Radio>
<Radio value='left'>Left</Radio>
</Stack>
</RadioGroup>
</>)
}
My question is, can this approach also be used in Vue? I don't seem to find any reference to it in the documentation. If this is not possible, is there a workaround for it?
答案1
得分: 3
我理解的是,你不能直接在Vue中像React中那样使用空标签语法 <></>
,但有替代方法可以实现类似的效果。
其中一个选项是使用 <template>
组件来包装多个元素,而不向DOM添加额外的标签。<template>
组件不会渲染为HTML标签,允许将元素分组在一起,而不会在结果元素树中添加另一个元素。
以下是如何使用它的示例:
<template>
<RadioGroup defaultValue={placement} onChange={setPlacement}>
<Stack direction='row' mb='4'>
<Radio value='top'>Top</Radio>
<Radio value='right'>Right</Radio>
<Radio value='bottom'>Bottom</Radio>
<Radio value='left'>Left</Radio>
</Stack>
</RadioGroup>
<RadioGroup defaultValue={placement} onChange={setPlacement}>
<Stack direction='row' mb='4'>
<Radio value='top'>Top</Radio>
<Radio value='right'>Right</Radio>
<Radio value='bottom'>Bottom</Radio>
<Radio value='left'>Left</Radio>
</Stack>
</RadioGroup>
</template>
请记住,在Vue中,保持组件的正确结构并确保元素按照Vue的语法规则正确嵌套是非常重要的。
英文:
My understanding is that you cannot directly use the empty tag syntax <></>
as in React, but there are alternatives to achieve a similar result in Vue.
One option is to use the <template>
component to wrap multiple elements without adding an extra tag to the DOM. The <template>
component does not render as an HTML tag and allows elements to be grouped together without adding another element to the resulting tree of elements.
Here's an example of how you could use it:
<template>
<RadioGroup defaultValue={placement} onChange={setPlacement}>
<Stack direction='row' mb='4'>
<Radio value='top'>Top</Radio>
<Radio value='right'>Right</Radio>
<Radio value='bottom'>Bottom</Radio>
<Radio value='left'>Left</Radio>
</Stack>
</RadioGroup>
<RadioGroup defaultValue={placement} onChange={setPlacement}>
<Stack direction='row' mb='4'>
<Radio value='top'>Top</Radio>
<Radio value='right'>Right</Radio>
<Radio value='bottom'>Bottom</Radio>
<Radio value='left'>Left</Radio>
</Stack>
</RadioGroup>
</template>
> Remember that in Vue, it is important to maintain a proper structure of the
> component and make sure elements are nested
> correctly according to Vue's syntax rules.
答案2
得分: 0
关于Vue中的碎片(没有直接的对应项)
https://blog.logrocket.com/fragments-in-vue-js/
还有这个https://www.npmjs.com/package/vue-fragment
英文:
about fragments in vue (there is no direct counterpart)
https://blog.logrocket.com/fragments-in-vue-js/
Also there is this https://www.npmjs.com/package/vue-fragment
答案3
得分: 0
在经过几周的对需求的挫折和调试后,我想出了一个非常简单、直接的解决方案,不需要我安装任何第三方库。
我创建了一个名为 VFragment
的新组件
将其添加为我的 main.js
中的全局组件,以便重复使用
该组件仅包含模板和默认插槽:
<template>
<slot></slot>
</template>
这样,我可以将 <v-fragment></v-fragment>
用作根组件,而不必增加额外的 div,并且可以将任何其他元素或组件直接放置在 <v-fragment></v-fragment>
的子元素中。
英文:
After a few weeks of being frustrated at the requirement and tinkering, I came up with a rather simple, straightforward solution that does not require me to install any third-party library.
I created a new component called VFragment
Added it as a global component in my main.js
for ease of reusability
The component contained only the template and a default slot:
<template>
<slot></slot>
</template>
This way, I can use <v-fragment></v-fragment>
as the root component and not have the bloat of an extra div while having any other elements or components as direct children of the <v-fragment></v-fragment>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论