功能按钮组件:
const Button = createElement('button', {}, [slots().icon ? createElement('span', slots().icon[0].text) : null])
另一个使用上面的Button的功能组件:
createElement(Button, {}, ['this goes to children'])
但是,当我将其包裹在上方时,这会导致文本this goes to children
不在span
内部。
如何将内容从另一个Button
放置在createElement()
组件的插槽中?
使用模板就足够简单了:
<template>
<Button>
<template #icon>This would be wrapped inside span</template>
</Button>
</template>
更新1
我必须在数据属性中使用slot: 'name-of-the-slot'
键:
const icon = createElement('span', { slot: 'icon' }, 'text inside span')
createElement(Button, {}, [icon])
没有成功。还能用吗?在Vue仓库中创建了错误报告:https://github.com/vuejs/vue/issues/11519
半解决方案
在Posva的帮助下:
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是关键。
如果该插槽存在,也不要忘记添加检查,例如:
return createElement("button", {}, scopedSlots.icon ? scopedSlots.icon(null) : null)
答案 0 :(得分:0)
使用模板的组件:
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>
在模板中使用 MyButton:
<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>
在渲染函数中使用它:
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)
},
}
从渲染函数中的作用域槽获取数据:
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
)
},
}
使用 jsx 或 js 实现 MyButton.vue
。
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>
)
},
}
在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>
)
},
}