vue.draggable.next icon indicating copy to clipboard operation
vue.draggable.next copied to clipboard

Weird DOM error

Open ct1735x opened this issue 3 years ago • 6 comments

I'm getting a strange red error, but only in the DOM. Vanilla console / Vue utility do not show any error. I'm using Vue 3 with Vuex 4 and VueDraggable 4.1.0

The error : image

I'm trying to setup a drag & drop nested list, here is my code :

<template>
  <draggable v-model="categories" item-key="tid" ghost-class="ghost">
    <template #item="{ category }">
      <div>{{ category.name }}</div>
      <drag-drop v-if="category.elements.length" v-model="category.elements" />
    </template>
  </draggable>
</template>
<script>
import draggable from 'vuedraggable'

export default {
  components: {
    draggable,
  },
  computed: {
    categories: {
      get() {
        return this.$store.state.categories.all
      },
      set(value) {
        this.$store.commit('updateSortedCategories', value)
      },
    },
  },
}
</script>

If I try to write {{ categories }}, we can see that the store get is not a problem :

[ 
{ 
   "tid": "2", 
    "name": "Installation & licences", 
    "elements": [ 
     { 
        "tid": "97", 
        "name": "Sub-category", 
         "elements": [] 
      } 
      ] 
}, 
{ 
     "tid": "4", 
     "name": "Tutorial & guides", 
      "elements": [] 
}, 
{ 
     "tid": "6", 
     "name": "Videos",
      "elements": [] 
} 
]

If have no idea why this is not working as intended, am I doing something wrong ?

ct1735x avatar Sep 03 '21 15:09 ct1735x

Same issue. Have you managed to fix it or find a workaround (or even a replacement)?

superkooks avatar Sep 27 '21 02:09 superkooks

@ClementT3 Your issue is multiple root nodes in the template. If you wrap them in a element it will go away.

@superkooks Probably the same, but you provided no reference template.

Something probably worth while doing is add some degree of error state/logging if someone does that.

Spice-King avatar Sep 27 '21 16:09 Spice-King

It turns out in my case I had set the slot wrong. It should be #item="{element}". Putting any other name for element caused it to error to DOM. It seems like this is what is causing @ClementT3's issue as well.

superkooks avatar Oct 01 '21 21:10 superkooks

Yea, you are destructuring an object with two properties, element and index. If you want to not use name element, you can rename the destructured variable like so #item="{element: car}". And for the sake of completeness with TS enabled, you can cast the type in the latest Vue 3 by doing this #item="{element: car} : {element: Car}", assuming Car is a type. Kinda ugly, but at least gets around needing generics to do it better.

Spice-King avatar Oct 01 '21 23:10 Spice-King

Thank you so much @Spice-King

Docs are unclear because coming from Vue 2 I had v-for="(chapter, index) in chapters" so I used #item="{chapter, chapterId}" and got no errors but empty objects...

brospars avatar Apr 27 '22 14:04 brospars

Yea, you are destructuring an object with two properties, element and index. If you want to not use name element, you can rename the destructured variable like so #item="{element: car}". And for the sake of completeness with TS enabled, you can cast the type in the latest Vue 3 by doing this #item="{element: car} : {element: Car}", assuming Car is a type. Kinda ugly, but at least gets around needing generics to do it better.

This should be part of the documentation! I made a PR to add this - see #171

cschra avatar Aug 11 '22 18:08 cschra