• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • renderToString()

    导出自vue/server-renderer


    类型

    function renderToString(
      input: App | VNode,
      context?: SSRContext
    ): Promise<string>
    


    例子

    import { createSSRApp } from 'vue'
    import { renderToString } from 'vue/server-renderer'
    
    const app = createSSRApp({
      data: () => ({ msg: 'hello' }),
      template: `<div>{{ msg }}</div>`
    })
    
    ;(async () => {
      const html = await renderToString(app)
      console.log(html)
    })()
    


    处理传送

    如果呈现的应用程序包含Teleport,则被传送的内容不会是呈现字符串的一部分。在大多数情况下,最好的解决方案是在安装时有条件地渲染Teleport

    如果确实需要远程传输内容Teleport,它们将在ssr上下文对象的teleports属性下公开:

    const ctx = {}
    const html = await renderToString(app, ctx)
    
    console.log(ctx.teleports) // { '#teleported': 'teleported content' }