I'm trying to solve this for Nuxt

Codesandbox of a WIP not working: 100

好的,我把WordPress作为CMS,它输出一堆HTML.HTML的一个示例如下所示:

'<h2>A heading tag</h2>
<site-banner image="{}" id="123">Slot text here</site-banner>
<p>some text</p>'

请注意,它包含一个Vue组件<site-banner>,上面有一些props (imageprops 是一个JSON对象,为了简洁起见我省略了).该组件已在全球注册.

我有一个我们编写的组件,名为<wp-content>,它在Vue中非常有效,但在Nuxt中不起作用.请注意这两个渲染函数,一个用于Vue,另一个用于Nuxt(显然这是为了示例,我不会同时使用这两个).

export default {
    props: {
        html: {
            type: String,
            default: ""
        }
    },
    render(h, context) {
        // Worked great in Vue
        return h({ template: this.html })
    }      
    render(createElement, context) {
        // Kind of works in Nuxt, but doesn't render Vue components at all
        return createElement("div", { domProps: { innerHTML: this.html } })
    } 
}

因此,最后一个呈现函数在Nuxt中工作,除了它实际上不会在this.html中呈现Vue组件,它只是将它们作为HTML放在页面上.

那么在Nuxt中我该怎么做呢?我想从服务器获取一个HTML字符串,并将其呈现在页面上,然后将任何已注册的Vue组件转换为适当的完整Vue组件.基本上是一个小小的"VueifyThis(html)"工厂.

推荐答案

这是最有效的,也是最干净的,多亏了Nuxt团队的乔纳斯·加尔维斯(Jonas Galvez),via oTechie.

export default {
  props: {
    html: {
      type: String,
      default: ""
    }
  },
  render(h) {
    return h({
      template: `<div>${this.html}</div>`
    });
  }
};

然后在你的nuxt.config.js文件中:

    build: {
        extend(config, ctx) {
            // Include the compiler version of Vue so that <component-name> works
            config.resolve.alias["vue$"] = "vue/dist/vue.esm.js"
        }
    }

Vue.js相关问答推荐

如何在与父集合相同的读取中取回子集合(或重构数据?)

为什么我的数组在 onBeforeMount 函数中不为空,但在 onMounted 函数中为空

有没有办法将react 键作为值传递给由 v-for 创建的 v-model

如何使用 vuejs 在浏览器 url 中获取当前 product.id 的详细信息

如何将字符串中的
解析为 VUE.js 中的 html 标记

webpack vue-loader 配置

Vuex:如何等待动作完成?

Vue Table 2 - 自定义过滤器

在下拉菜单上使用箭头键滚动

在 vue 组件中检索配置和环境变量

如何获取 Vue 路由历史记录?

更新 v-for 中使用的数组内的对象时,Vue js react性问题

单击复选框后,V-model 未更新

如何使用 vuex 删除项目?

请求正在进行时显示加载器

如何在 NPM 上创建和发布 Vuejs 组件

使用props向组件添加类名

所有 vue props和数据都给出错误 Property属性在 type 上不存在,带有 typescript

使用 Vue 在单个文件组件中调用渲染方法

使用 vuejs 动态设置 id 标签