首页 > 解决方案 > VueJS - 如何在 HTML 字符串中转换 this.$slots.default?

问题描述

我使用 Vue CLI 创建了一个项目,现在我需要挂载函数中的插槽值。

我可以使用this.$slots.default访问插槽,并且我得到了 VNode 数组。

现在我需要将 VNode 数组转换为 HTML 字符串,例如:

const html = `<p><strong>Lorem</strong> ipsum</p>`;

我目前的代码如下所示:

<TestComponent field="website" :data="document">
    <p><strong>Lorem</strong> ipsum</p>
</TestComponent>


<template>
  <div class="test">
        <p>lorem ipsum</p>
  </div>
</template>

<script>
export default {
  props: ["field", "data"],
  data() {
    return {
      editor: null,
    };
  },
  mounted() {
    if (this.$slots.default) {
      const html;
      console.log(this.$slots.default);
    }
  },
};
</script>

标签: javascriptvue.jsslots

解决方案


推荐阅读