vue.js - 在 vue.js 中提供给 v-for 用于迭代的未闭合标签是否有效?
问题描述
假设,我有这样的 vue 代码:
<template>
<div v-for="(item, index) in items">
<div v-if="item.isComponent">
<component :is="item.value"/>
</div>
<template v-else>
{{item.value}}
</template>
</div>
</template>
<script>
export default {
data: function(){
return {
items: [
{
value: '<p>this is first part of paragraph
},
{
value: 'componentName',
isComponent: true,
},
{
value: 'this is the last part of paragraph</p>
},
],
//...
</script>
items
contenteditable
- 它是标签编辑器的已解析(我尚未解析)字符串。
如果这是无效的,可能有什么解决方法?
UPD。
items
是我将从数据库中获取的 json,它应该保存到数据库作为用户输入到contenteditable
div 编辑器。
解决方案
Html 将清理 Html 部分。所以这不是一个好主意。做这样的事情。html 应该保存在 vue 的模板中。但是假设你想显示一些数据
标签。而不是使用计算的道具并隐藏和显示 p 标签。它还可以防止不完整的标签问题。
我正在附加 jsfiddle 解决方案[
new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!',
items: [
{
value: '<p>this is first part of paragraph'
},
{
value: 'componentName',
isComponent: true,
},
{
value: 'this is the last part of paragraph</p>'
},
],
},
computed:{
dataVal(){
let val = "";
for(let i=0;i<this.items.length;i++){
val += this.items[i].value + " "
}
return val;
}
}
})
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
del {
color: rgba(0, 0, 0, 0.3);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app" v-html="dataVal">
</div>
] 1
推荐阅读
- typescript - 在 vscode 中调试共享打字稿模块
- azure - Ansible - Azure 资源创建 - 动态名称
- reactjs - 一系列链式 http 请求的反应钩子和 RTK 模式
- r - 如何将 phytools 表形图保存为图像?
- excel - 字符串 IF ELSE 函数超出 Excel 字符数限制?
- python - 在我的组织内连接到 Google API
- php - 如何使用运算符和 && 显示“某物”
- javascript - 一个组件可以在另一个非父子组件的 onClick 上使用吗?
- python - 此 RandomForestClassifier 实例尚未安装
- python - 用于标记分类的 Huggingface DistilBert 不会提高训练期间的准确性