【问题标题】:How to use slot in Vue 2 render function?如何在 Vue 2 渲染功能中使用插槽?
【发布时间】:2021-04-16 03:29:35
【问题描述】:

我想执行以下操作,但使用 Vue 2 的渲染功能

<template>
  <imported-component>
    <template v-slot:default="{ importedFunction }">
       <button @click="importedFunction">Do something</button>
    </template>
  </import-component>
</template>

【问题讨论】:

    标签: javascript vue.js vuejs2 vue-component vue-render-function


    【解决方案1】:
    • 使用scopedSlots 作为插槽
    • 使用插槽名称(“默认”)加上插槽道具的函数参数
    • 使用on 作为事件处理程序
    render(h) {
      return h('imported-component', {
        scopedSlots: {
          default(slotProps) {
            return h('button', {
              on: {
                click: slotProps.importedFunction
              }
            }, 'Do something')
          }
        }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2019-10-26
      • 2020-10-26
      • 2021-07-28
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2017-02-27
      • 2022-10-06
      相关资源
      最近更新 更多