将VNode渲染为html字符串

时间:2018-12-17 02:07:05

标签: javascript vue.js

我正在开发Vue服务器端模板引擎,其中的一部分包含一个头管理系统。

我正在为Vue开发服务器端渲染器,为了使SSR的头部管理正常工作,它需要一种将VNode渲染为文本的方法。

我的问题: 如何将VNode渲染为字符串?例如:

this.$ssrContext.head = renderVNodesToString(this.$slots.head)

用例示例:

master.vue

<template>
    <div id="app">
        <slot name="content"></slot>
    </div>
</template>
<script>
export default {
    created: function(){
        if(this.$isServer){
            var VNodesToRender = this.$slots.head
            this.$ssrContext.head = 'rendered VNode here'
        }
    }
}
</script>

home.vue

<template>
    <master>
        <template slot="content">
            Hello World
        </template>
        <template slot="head">
            <script src="https://unpkg.com/vue/dist/vue.js"></script>
            <title>Hello</title>
        </template>
    </master>
</template>
<script>
import master from "layouts/master.vue"

export default {
    components: {
        master
    }
}
</script>

我的目标是将home.vue的{​​{1}}槽呈现为字符串,并将其注入到head中,以便可以在服务器端读取和注入

this.$ssrContext中,我可以毫无问题地访问master.vue,其中包含正确的this.$slots.head s

使用字符串的地方

VNode

{{{ head }}}

注意: 这个问题不是这样的:

  1. VueJS Render VNode
  2. Understanding Vnodes
  3. Can you render VNodes in a Vue template?

因为它需要一个字符串输出,该字符串将传递到SSR

0 个答案:

没有答案