javascript - 如何在 Vue 2 渲染函数中使用插槽?

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

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

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

最佳答案

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

关于javascript - 如何在 Vue 2 渲染函数中使用插槽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65662621/

相关文章:

javascript - JQuery/Microsoft 模板和链接 : Did they ever make these work together?

javascript - 如何修复vuejs中的 "ReferenceError: require is not defined"

javascript - 如何检查一个对象是否是一个 Vue 组件?

vuejs2 - vuejs 组件创建等待数据首先加载

javascript - 清空 Vuex 对象时,如何避免 Vue 项目出现未定义错误?

javascript - 我正在使用 this.$router.push 以编程方式导航,但是,我被发送到错误的 URL,我不确定为什么

javascript - 如何检查元素类型?

javascript - JQuery Fileupload 带有表单数据并且只有单个文件

javascript - 更改spynner用户代理不起作用

javascript - 带有 Vanilla JS 的 Vue-Component 内的 addEventListener