【发布时间】:2021-07-28 11:11:53
【问题描述】:
我正在创建一个像这样的动态降价组件
<div v-highlight :is="markdownComponent"></div>
在计算中:
computed: {
markdownComponent() {
return {
template: this.html,
data() {
return {}
},
methods: { }
}
}
}
this.html 是使用 markdown-it 动态创建的。我创建了一个 code_block/fence 规则来增强 pre > code 块。 this.html 的一个例子是:
<div>
<div class="code-header">
<span class="code-language">
Python
</span>
<a class="code-copy" @click="copyText('xxxx')">
<i class="fa fa-copy"></i>
Copy
</a>
</div>
<pre>highlighted data</pre>
</div>
它可以工作,但 VUE 会打印一个警告
[Vue warn]: Failed to mount component: template or render function not defined.
found in
---> <Anonymous>
<Notes2> at src/note.vue
<App> at src/App.vue
<Root>
如果我添加一个空的渲染函数,警告将会消失,但页面也会被清空:
computed: {
markdownComponent() {
return {
render(h) {
return h('div', {}, [])
},
template: this.html,
data() {
return {}
},
methods: { }
}
}
}
}
如何添加“默认”渲染功能?我需要在停止 VUE 打印该警告的同时正确呈现页面。
编辑
我已经检查过Vue template or render function not defined yet I am using neither? 它没有提供解决此问题的方法
【问题讨论】:
-
geauser 可能是对的。什么是
this.html- 它来自哪里以及它的外观如何?还为您正在使用的 Vue 版本添加适当的标签... -
@MichalLevý 它是从 markdown-it 动态创建的。我只需要在“pre > code”元素中添加一个复制按钮。我在问题中附上了一个例子
-
现在,检查哪个 Vue 包 you are using
-
另外注意@daisy,您可以简单地修改现有元素的
innerHTML,而不是为您的降价语料库创建新组件。我可能会让你的代码更清晰。
标签: javascript vue.js