Javascript 将VNode呈现为html字符串
我正在开发一个Vue服务器端模板引擎,包的一部分包括一个头部管理系统 我正在为Vue开发一个服务器端渲染器,为了让SSR的头部管理工作,它需要一种将Javascript 将VNode呈现为html字符串,javascript,vue.js,Javascript,Vue.js,我正在开发一个Vue服务器端模板引擎,包的一部分包括一个头部管理系统 我正在为Vue开发一个服务器端渲染器,为了让SSR的头部管理工作,它需要一种将VNodes渲染为文本的方法 我的问题: 如何将VNode渲染为字符串?例如: this.$ssrContext.head=rendervNodesString(this.$slots.head) 示例用例: master.vue <template> <div id="app"> <slot n
VNode
s渲染为文本的方法
我的问题:
如何将VNode渲染为字符串?例如:
this.$ssrContext.head=rendervNodesString(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>
<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
的head
槽渲染成字符串,并将其注入this.$ssrContext
以便可以在服务器端读取和注入
在master.vue
中,我可以毫无问题地访问this.$slots.head
,它包含正确的VNode
s
使用字符串的位置
在{{{head}}}
const renderer = createBundleRenderer(bundle.server, {
runInNewContext: false,
inject: false,
template: `<!DOCTYPE html>
<html>
<head>
{{{ head }}}
{{{ renderResourceHints() }}}
{{{ renderStyles() }}}
</head>
<body>
<!--vue-ssr-outlet-->
<script>${ bundle.client }</script>
</body>
</html>`
})
const renderer=createBundleRenderer(bundle.server{
runInNewContext:false,
注:错,
模板:`
{{{head}}}
{{{renderResourceHitts()}}}
{{{renderStyles()}}}}
${bundle.client}
`
})
注意:
这个问题不像:
因为它需要一个字符串输出,然后将其传递给SSR,我想您需要的是。它可能是
vue服务器渲染器
包。似乎可以渲染任何东西。@哦,很不幸,这没什么帮助。我已经在使用它来渲染.vue文件。我正在尝试将一个变量从.vue文件传递到$ssrContext