javascript - 将 VNode 渲染为 html 字符串
问题描述
我正在开发一个 Vue 服务端模板引擎,包的一部分包括一个头部管理系统。
我正在为 Vue 和 SSR 的头部管理开发一个服务器端渲染器,它需要一种将VNode
s 渲染为文本的方法。
我的问题: 如何将 VNode(s) 呈现为字符串?例如:
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
' 的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>`
})
注意: 这个问题不像:
因为它需要一个字符串输出,它被传递给 SSR
解决方案
推荐阅读
- python - 在列表中的项目中移动字符的位置
- image - 在 ImageJ 中应用灰度形态学关闭
- bash - 本机终端外壳中缺少 Bash 提示
- odata - C# OData GetKeyFromUri - 找不到段“odata”的资源
- arrays - 在 VBA 中解析特定 JSON 时出现问题
- bash - 循环让 youtube-dl 下载其中一种格式(取决于错误)
- c# - 如何让角色在统一跳跃的同时移动
- javascript - 通过单选按钮“视频选择屏幕”
- r - 通过 R 中的 SpatialPolygonsDataFrame 对象列表提取栅格
- node.js - “.Strategy”在 Node 或 Passport 中有什么作用?