简体   繁体   English

在 Vue 中显示 DocumentFragment

[英]Display a DocumentFragment in Vue

I am using a library (prosemirror) that gives me a DocumentFragment out of json I get from the database.我正在使用一个库(prosemirror),它为我提供了一个来自数据库的 json 的 DocumentFragment。 Now I want to display it in my component.现在我想在我的组件中显示它。 How?如何? Two hours of googling gave me nothing.两个小时的谷歌搜索没有给我任何东西。

  resolveContent: function () {
    let contentNode = schema.nodeFromJSON(content)
    let fragment = DOMSerializer.fromSchema(schema).serializeFragment(contentNode.content)
    return fragment
  }

Using this in my component like在我的组件中使用它

  p(v-html="resolveText()")

or或者

  p {{resolveText()}}

Prints印刷

  [object DocumentFragment]

To handle DocumentFragment within Vue.js, you will need to wait until Vue's mounted hook fires.要在 Vue.js 中处理DocumentFragment ,您需要等到 Vue 的mounted钩子触发。 I would create a wrapper component which "renders" simply an empty <div/> .我会创建一个包装器组件,它只是“呈现”一个空的<div/> Then, hook in to mounted and insert the fragment using standard DOM commands (eg. Node.appendChild() )然后,使用标准 DOM 命令(例如Node.appendChild()mounted并插入片段

const FragmentBootstrapper = Vue.extend({
  mounted() {
    const fragment = resolveContent()

    this.$el.appendChild(fragment)
  },
  render(h) {
    return h("div") // becomes `this.$el`
  }
}

This may feel weird at first, but it makes the most sense when you consider that Vue is is rendering layer abstraction .起初这可能会觉得很奇怪,但当你考虑到 Vue 是在渲染层抽象时,这才最有意义。 Thus, it during its own rendering hook, it expects to only deal with its own VDOM.因此,在它自己的渲染钩子中,它期望只处理它自己的 VDOM。 It provides the mounted hook in order to support various use-cases, namely this one.它提供了mounted的钩子以支持各种用例,即这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM