vue-frag provides Vue 3's native fragment functionality to Vue 2 applications, allowing components to render multiple root elements without an artificial wrapper div. This addresses a core limitation in Vue 2's template syntax. The current stable version is 1.4.3. Releases are made on an as-needed basis, primarily for bug fixes and minor enhancements related to Vue 2 compatibility. Key differentiators include support for server-side rendering (SSR), compatibility with core Vue directives like `v-if`, `v-for`, and `v-html`, and offering both a Component API (`<fragment>`) and a Directive API (`v-frag`) for flexible usage. It can also be paired with `vue-frag-plugin` for automatic fragment injection, simplifying development by allowing multiple root nodes directly in SFCs.
npm install vue-fragVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates using the `Fragment` component to allow multiple root `<li>` and `<div>` elements within a Vue 2 component's template, which would otherwise require a single wrapper element.
For Vue 3, simply remove the `<fragment>` component or `v-frag` directive and declare multiple root nodes directly in your template.
Ensure `import { Fragment } from 'vue-frag';` and `components: { Fragment }` (or `import frag from 'vue-frag';` and `directives: { frag }`) are present, or register globally once in your main application file.Upgrade to `vue-frag` version 1.4.3 or newer to resolve `keep-alive` compatibility issues.
Understand that `v-frag` will make the element it's applied to disappear from the DOM. Use it when you explicitly want to unwrap a component's single root element, allowing its children to become direct children of its parent.
Ensure you have `import { Fragment } from 'vue-frag';` in your script and added `Fragment` to your component's `components` option, or globally registered it with `Vue.component('Fragment', Fragment);`.Ensure you have `import frag from 'vue-frag';` in your script and added `frag` to your component's `directives` option, or globally registered it with `Vue.directive('frag', frag);`.Upgrade `vue-frag` to the latest version (1.4.3 or newer) as several bug fixes in 1.2.x and 1.4.x releases addressed these specific issues related to DOM patching and sibling detection.