1. vue-fragment
fragment component for vuejs
vue-fragment
Package: vue-fragment
Created by: Thunberg087
Last modified: Tue, 31 May 2022 12:26:08 GMT
Version: 1.6.0
License: MIT
Downloads: 164,617
Repository: https://github.com/Thunberg087/vue-fragment

Install

npm install vue-fragment
yarn add vue-fragment

vue-fragment npm version


what

a very candide fragment component for Vue.js

why

If you arrived here, I think you searched hard and you know why you're here.

For others, fragments are basically root-less components. They come useful in many situations where you don't want to pollute the DOM with a useless container, or you want to return many elements at once.

how

It's impossible to use functional components or slots, since Vue.js vDOM diffing has a "you should return one root element" limitation… Instead, I'm using an (internal) directive which will dump all the children of the fragment root node in its place. Since directives can manipulate DOM, we can act after rendering and bypass Vue limitation.

The component is called Fragment so you won't have to change much code when Vue3 native fragments arrive. That said, I'm not a core developer of Vue.js, and I don't have any view of their implementation. I only know what fragments are and how they should work, and did my best to reproduce it ; so it should be fine.

use

  • download the package npm i -s vue-fragment

From here, you can:

  • Plugin:

     import Fragment from 'vue-fragment'
    Vue.use(Fragment.Plugin)
    
    // or
    
    import { Plugin } from 'vue-fragment'
    Vue.use(Plugin)
    
    // …
    
    export const MyComponent {
      template: '
      <fragment>
        <input type="text" v-model="message">
        <span>{{ message }}</span>
      </fragment>
      ',
      data() { return { message: 'hello world }}
    }
    
  • Component:

     import { Fragment } from 'vue-fragment'
    
    export const MyComponent {
      components: { Fragment },
      template: '
      <fragment>
        <input type="text" v-model="message">
        <span>{{ message }}</span>
      </fragment>
      ',
      data() { return { message: 'hello world }}
    }
    

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin