vue.js - 将插槽从 Vue 2 迁移到 Vue 3

我大致跟着this article使组件可拖动:

<template>
  <div ref="draggableContainer" class="draggable-container">
    <div class="draggable-header" @mousedown="dragMouseDown">
      <slot name="dragger"></slot>
    </div>
    <slot></slot>
  </div>
</template>

然后在我的 Calculator.vue 组件中我有:

<template>
  <Draggable class="calculator">
    <input type="text" class="calculator-screen" slot="dragger" value="" />

    <div class="calculator-keys">
      <button type="button" class="operator" value="+">+</button>
      <button type="button" class="operator" value="-">-</button>
      <button type="button" class="operator" value="*">&times;</button>
      <button type="button" class="operator" value="/">&divide;</button>

      <button type="button" value="7">7</button>
      <button type="button" value="8">8</button>
      <button type="button" value="9">9</button>

      <button type="button" value="4">4</button>
      <button type="button" value="5">5</button>
      <button type="button" value="6">6</button>

      <button type="button" value="1">1</button>
      <button type="button" value="2">2</button>
      <button type="button" value="3">3</button>

      <button type="button" value="0">0</button>
      <button type="button" class="decimal" value=".">.</button>
      <button type="button" class="all-clear" value="all-clear">AC</button>

      <button type="button" class="equal-sign operator" value="=">=</button>
    </div>
  </Draggable>
</template>

这两个组件以不同的方式使用 slot,在 draggable-maker 中作为标签,在计算器中作为属性。但是,由于使用了 slot,这与 Vue 3 不兼容。这是我收到的错误:

有人可以建议我如何解决这个问题吗?

最佳答案

slot 属性已被弃用,它被 v-slot:slotname 替换为命名插槽,您应该按如下方式使用它:

  <Draggable class="calculator">
   <template v-slot:dragger>

   <input type="text" class="calculator-screen" value="" />
    <div class="calculator-keys">
      <button type="button" class="operator" value="+">+</button>
      <button type="button" class="operator" value="-">-</button>
      <button type="button" class="operator" value="*">&times;</button>
      <button type="button" class="operator" value="/">&divide;</button>

      <button type="button" value="7">7</button>
      <button type="button" value="8">8</button>
      <button type="button" value="9">9</button>

      <button type="button" value="4">4</button>
      <button type="button" value="5">5</button>
      <button type="button" value="6">6</button>

      <button type="button" value="1">1</button>
      <button type="button" value="2">2</button>
      <button type="button" value="3">3</button>

      <button type="button" value="0">0</button>
      <button type="button" class="decimal" value=".">.</button>
      <button type="button" class="all-clear" value="all-clear">AC</button>

      <button type="button" class="equal-sign operator" value="=">=</button>
    </div>
</template>
  </Draggable>

不要忘记从 input 元素中删除 slot="dragger",您使用的语法已从 2.6.0 版开始弃用,其中将包含 v3

https://stackoverflow.com/questions/63749380/

相关文章:

c - main的地址是什么?

python - 在列表字典中查找最大列表范围的更好(更简洁)方法是什么

r - separate_rows 在结果周围生成引号

python - Python 中的日期字符串格式化

python - 类型错误 : request() missing 1 required posit

java - 阵列旋转 TLE(超出时间限制)

python - 如何在 Python 中订阅 NATS 主题并继续接收消息?

scala - 如何在不分配给 val 的情况下使用隐式调用返回的函数

html - 轮播滑动动画不适用于 Bootstrap 4.5.2

angular - 使用@input 对 Angular 组件进行单元测试