要真正“返回呈现的 HTML”,您需要使用generate: 'ssr'
option进行编译,并使用该Cmp.render()
函数。但是你不能在 DOM 中使用这样的组件......
您无法真正获取组件的内容,但您可以使用以下操作获取任何元素的 HTML:
<script>
const logHtml = el => {
console.log(el.innerHTML)
}
</script>
<div use:logHtml> ... </div>
或者,简单地说,bind:this
在元素上:
<script>
let el
$: if (el) console.log(el.innerHTML)
</script>
<div bind:this={el}> ... </div>
但是,在您的情况下,您似乎想要将组件直接呈现到工具提示的元素中。你可以使用new MyComponent
它。
这是一个渲染自定义组件的示例操作tippy.js
(请参阅更新的 REPL):
export function tipz(elem, { content, props, ...opts }) {
let cmp
const tp = tippy(elem, {
onCreate() {
cmp = new content({
target: instance.popper.querySelector(".tippy-content"),
props,
});
},
...opts
})
return {
update(params) {
// ensure reactivity
if (cmp) {
cmp.$set(params.props)
}
},
destroy() {
tp.destroy();
if (cmp) {
// cleanup component
cmp.$destroy();
}
}
};
}