【问题标题】:How do I pass the div element outside setup function如何在设置函数之外传递 div 元素
【发布时间】:2023-03-13 21:18:01
【问题描述】:

我正在尝试将此添加到我的 vue 应用程序中 https://jsbin.com/yejehog/edit?html,js,console,output

我已添加到我的代码中,如何将 div id 传递给 Sortable.create? 在我的控制台中,我收到错误 Uncaught Sortable: el` must be an HTMLElement, not [object Null]

import { Sortable, Swap } from 'sortablejs/modular/sortable.core.esm';
Sortable.mount(new Swap());

let list = document.getElementById("list")

Sortable.create(list, {
  multiDrag: true,
  selectedClass: "selected"
});
 <div id="list" v-for="(item, index) in points.pointList.collection">
<p>{{ item.name}}</p>
</div>

这个也试过了

    const clueDiv = ref(null)

  Sortable.mount(new Swap());

    Sortable.create(clueDiv.value, {
      multiDrag: true,
      selectedClass: "selected"
    });
    #list(v-for="(item, index) in points.pointList.collection" ref="clueDiv")

同样的错误

【问题讨论】:

    标签: javascript vue.js vuejs3


    【解决方案1】:

    首先应将list id 添加到包含使用 v-for 呈现的项目的包装器元素中,然后使用 onMounted 挂钩运行 init 可排序函数:

    <div id="list" ref="clueDiv">
      <div  v-for="(item, index) in points.pointList.collection">
       <p>{{ item.name}}</p>
      </div>
    </div>
    

    和:

     const clueDiv = ref(null)
    
    onMounted(()=>{
     Sortable.mount(new Swap());
    
        Sortable.create(clueDiv.value, {
          multiDrag: true,
          selectedClass: "selected"
        });
    })
     
    

    【讨论】:

    • 我仍然收到 Uncaught (in promise) Sortable: el` 必须是 HTMLElement,而不是 [object Null]` 出于某种原因
    • 谢谢你解决了,还有一个问题。如果我想使用sortablejs.github.io/Sortable/#filter 过滤器的东西,但我必须在那里提供一个类,你会怎么做?我还需要为此使用 ref 吗?
    • 根据&lt;div v-for="(item, index) in points.pointList.collection" :class="{'filtered':index===2}"&gt;等条件使用类,然后使用示例中提到的.filtered类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多