当我尝试更新自定义文本区域组件的模型数据时this.message='<span id="foo">bar</span>,文本和html不会像hjecttextarea标签那样显示,但我可以在Vue开发工具的控制台中看到更新.我也试过切换到一个对象而不是字符串并使用Vue.set,但这也不起作用.
对于如何解决这个问题,有任何的建议吗?
该htmlTextArea组件的目标是从htmlTextArea标签获取用户文本(这可行),操作此文本并将其绑定回textarea,但其中包含HTML.
自定义文本区域组件:
<template>
<div contenteditable="true" @input="updateHTML" class="textareaRoot"></div>
</template>
<script>
export default {
// Custom textarea
name: 'htmlTextArea',
props:['value'],
mounted: function () {
this.$el.innerHTML = this.value;
},
methods: {
updateHTML: function(e) {
this.$emit('input', e.target.innerHTML);
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
其他组件:
<template>
...
<htmlTextArea id="textarea" v-model="message"></htmlTextArea>
...
</template>
<script>
data: {
return {
message: 'something'//this works
}
}
...
methods: {
changeText() {
this.message='<span id="foo">bar</span>'//this does not
}
},
components: {
htmlTextArea
}
</script>
Run Code Online (Sandbox Code Playgroud)
您需要在value道具更改后明确设置值.你可以看的value变化.
<template>
<div contenteditable="true" @input="updateHTML" class="textareaRoot"></div>
</template>
<script>
export default {
// Custom textarea
name: "htmlTextArea",
props: ["value"],
mounted: function() {
this.$el.innerHTML = this.value;
},
watch: {
value(v) {
this.$el.innerHTML = v;
}
},
methods: {
updateHTML: function(e) {
this.$emit("input", e.target.innerHTML);
}
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8212 次 |
| 最近记录: |