简体   繁体   中英

How to pass content to a slot in render function (functional component) - Vue

Functional Button component:

const Button = createElement('button', {}, [slots().icon ? createElement('span', slots().icon[0].text) : null])

Another functional component that uses Button above:

createElement(Button, {}, ['this goes to children'])

But this renders text this goes to children not inside the span as i wrapped it above. How can i put contents inside a slot in a Button component from another createElement() ?

With templates it's easy enough:

<template>
  <Button>
    <template #icon>This would be wrapped inside span</template>
  </Button>
</template>

UPDATE 1

I've to utilize slot: 'name-of-the-slot' key in the data properties:

const icon = createElement('span', { slot: 'icon' }, 'text inside span')
createElement(Button, {}, [icon])

No success. Does it even work? Created a bug report in a Vue repo: https://github.com/vuejs/vue/issues/11519

SEMI-SOLUTION

With the Posva's help:

export default {
  name: "Wrapper",
  functional: true,
  render(h) {
    return h(Button, { scopedSlots: {
      icon: () => h('span', {}, 'its from wrapper')
    } });
  }
};

export default {
  name: "Button",
  functional: true,
  render(createElement, { scopedSlots }) {
    return createElement("button", {}, scopedSlots.icon(null));
  }
};

ScopedSlots was the key.

Also don't forget to add check, if this slot exists, like:

return createElement("button", {}, scopedSlots.icon ? scopedSlots.icon(null) : null)

A component using template:

MyButton.vue

<template>
  <div>
    <slot name="left"></slot>
    <button>
      <slot v-bind:person="person">
        <span>按钮</span>
      </slot>
    </button>
    <slot name="right" v-bind:age="person.age"></slot>
  </div>
</template>

<script>
  export default {
    name: 'MyButton',
    data() {
      return {
        person: {
          name: 'jack',
          age: 23,
        },
      }
    },
  }
</script>

use MyButton in template:

 <MyButton>
   <template #right="{age}">
     <span>button rigt side. I am  {{ age }} years</span>
   </template>
   <template v-slot="{ person }">this is a button,{{ person }}</template>
   <template #left>
     <span>button left side</span>
   </template>
</MyButton>

use it in render function:

import MyButton from './MyButton.vue'
export default {
  name: 'UseButton',
  render(h) {
    const slotLeft = h('template', { slot: 'left' }, 'in the left of button')
    const slotRight = h('template', { slot: 'right' }, 'right')
    const slotDefault = h('template', { slot: 'default' }, 'default slot')
    const children = [slotRight, slotLeft, slotDefault]
    // normal slots in third parms, any order work well
    return h(MyButton, {}, children)
  },
}

get data from scoped slot in render function:

import MyButton from './MyButton.vue'
export default {
  name: 'UseButton',
  render(h) {
    const slotLeft = h('template', { slot: 'left' }, 'normal slot with name left')
    const children = [slotLeft]
    return h(
      MyButton,
      {
       // scopedSlot in the second param
        scopedSlots: {
          // props come from MyButton inside
          default: props => {
            console.log(props)
            const { person } = props
            const text = `defaul scopedSlot,${JSON.stringify(person)}`
            // use h
            return h('span', {}, text)
          },
          right: props => {
            console.log(props)
            const { age } = props
            // use jsx
            return <span>in the right,I am {age} years</span>
          },
        },
      },
      // normal slot
      children
    )
  },
}

implement of MyButton.vue with jsx or js.

MyButton.jsx

export default {
  name: 'MyButton',
  data() {
    return {
      person: {
        name: 'jack',
        age: 23,
      },
    }
  },
  render(h) {
    const { left, right, default: _defaultSlot } = this.$scopedSlots
    // checek the default exist or not
    const defaultSlot = (_defaultSlot && _defaultSlot({ person: this.person })) || <span>按钮</span>
    const leftSlot = (left && left()) || ''
    const rightSlot = right(this.person)
    const button = h('button', {}, [defaultSlot])
    //  jsx make structure more clear
    return (
      <div>
        {leftSlot}
        {button}
        {rightSlot}
      </div>
    )
  },
}

use functional component with jsx:

FunctionalButton.jsx

export default {
  name: 'FunctionalButton',
  functional: true,
  props: {
    person: {
      type: Object,
      default: () => ({ name: 'jack', age: 23 }),
    },
  },
  // NO DATA in functional component
  // data() {
  //   return {
  //     person: {
  //       name: 'jack',
  //       age: 23,
  //     },
  //   }
  // },
  render(h, { props, scopedSlots }) {
    const { left, right, default: _defaultSlot } = scopedSlots
    const defaultSlot = (_defaultSlot && _defaultSlot({ person: props.person })) || <span>按钮</span>
    const leftSlot = (left && left()) || ''
    const rightSlot = right(props.person)
    const button = h('button', {}, [defaultSlot])
    return (
      <div>
        {leftSlot}
        {button}
        {rightSlot}
      </div>
    )
  },
}

Using dynamic component resolved a problem for me, you can simply pass render function into "is" attribute like this

<div v-for="slot in a.tabs"><component :is="slot.renderFn"></component></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM