【发布时间】:2021-07-06 12:12:26
【问题描述】:
我需要展示通过props传递的组件,并通过v-html输出。我找到了一个渲染解决方案。但我不知道如何在本地组件中应用它,因为我使用的是 nuxt。 Vue.compile(this.vcontent);报错
<script>
export default {
props: ['vcontent'],
data() {
return {
templateRender: null,
}
},
render(h) {
if (!this.templateRender) {
return h('div', 'loading...');
} else { // If there is a template, I'll show it
return this.templateRender();
}
},
watch: {
vcontent:{
immediate: true, // makes the watcher fire on first render, too.
handler() {
var res = Vue.compile(this.vcontent);
this.templateRender = res.render;
// staticRenderFns belong into $options,
// appearantly
this.$options.staticRenderFns = []
// clean the cache of static elements
// this is a cache with the results from the staticRenderFns
this._staticTrees = []
// Fill it with the new staticRenderFns
for (var i in res.staticRenderFns) {
//staticRenderFns.push(res.staticRenderFns[i]);
this.$options.staticRenderFns.push(res.staticRenderFns[i])
}
}
}
},
}
</script>
如果我尝试在组件中导入 Vue,webpack 会报错 vue__WEBPACK_IMPORTED_MODULE_0___default.a.compile is not a function
【问题讨论】: