javascript - 如何将 HTML 代码分配给 Vue.js 中的变量
问题描述
我想在我的 Vue.js 3setup()
函数中的变量中包含一些 HTML,以便稍后在模板中使用。
当我尝试以下操作时,我收到一个错误,"VueCompilerError: Invalid end tag."
因为 Vue 似乎认为它是组件代码的一部分(当然不是)。
<script>
imports...
setup() {
const a = a;
const b = b;
const MyHTML = '<script></script>' // Vue thinks the </script> tag is part of the code
}
</script> // This gets thrown an error of VueCompilerError: Invalid end tag.
然后,如何在不被 Vue 解释的情况下将 HTML 作为字符串存储在变量中?
解决方案
我对此进行了搜索并找到了几种解决方案。解决方案之一是在线下方。我认为这是最好的解决方案
<div v-html="MyHTML"></div>
// eslint-disable-next-line no-useless-escape
const MyHTML = ref('<script><\/script>')
另一个有趣的解决方案是
const MyHTML = '...... </scr'+'ipt>......';
推荐阅读
- width - 如何在 Blueprintjs 中使用(或模拟)填充进行选择?甚至是固定宽度?
- python - pip 包的 Python 目录不起作用
- php - laravel 中 Eager Loading 字段的变化
- python - try:'spam'[2] 除了 IndexError:print('exception') ==> 为什么不是 'a'?
- plot - 绘制顶点定位类似于其他图形的图形
- javascript - 未定义不是对象“this.props.navigation.navigate”
- sql - VACCUM FULL 命令未收集死元组
- dictionary - Groovy 说采用 java.util.Map 的方法不能采用 HashMap 或 LinkedHashMap
- php - 安装couchbase php sdk后,无法添加php模块
- r - 如何将一行的值与R中不同列中的另一行直接匹配