javascript - Vue 3 渲染动态评论
问题描述
如何在 Vue 3 中正确呈现动态评论?我尝试了在我的情况下不起作用的 v-html,例如https://i.imgur.com/EtrVmGu.png
<template>
<!-- Method 1: not working properly, https://i.imgur.com/EtrVmGu.png -->
<template v-html="COMMENT" />
<!-- Method 2: does not solve the problem, print as string -->
{{ COMMENT }}
content here
<!--[if mso | IE]> </span> <![endif]-->
</template>
<script>
export default {
setup() {
const COLOR = "#FF0000";
const COMMENT = `<!--[if mso | IE]> <span style="background: ${COLOR}"> <![endif]-->`;
return {
COMMENT
}
}
}
<script>
<div v-html="">
不会解决问题,请参阅:https ://i.imgur.com/6tqaQAe.png
解决方案
这对我有用。
<template> <h1>Dynamic Component</h1> <div v-html="COMMENT"></div> </template> <script> export default { setup() { const COLOR = "#FF0000"; const COMMENT = `<span style="background: ${COLOR}">Comment</span>`; return { COMMENT, }; }, }; </script>
推荐阅读
- java - Java struts recaptcha v2 无法正常工作
- ios - 为什么 md-select-on-focus 在 ios 中不起作用?
- javascript - amcharts中的图表容器宽度和高度动态
- unit-testing - 如何获得一个可以抛出我正在使用 XCTAssertNoThrow(...) 测试的函数的返回值
- vue.js - 带有 Laravel 5.5 的 Vue 单文件组件
- python - Kafka生产者flush和poll的区别
- java - 根据标签值的变化将 xml 拆分为更小的
- android - Android Fragment onClick 按钮打开具有相同活动的新 Fragment
- java - 无法使用无头模式 Selenium 定位元素
- excel - 如果用户尝试在受保护的单元格中写入,则自定义消息