问题描述
我正在尝试在表单文本输入中显示一些html实体,但是v-model
似乎在转义它们.
I'm trying to display some html entities in a form text input, but v-model
seems escaping them.
我需要写些什么来使v-model
正确显示html实体吗?
Is there something I need to write to make v-model
displaying correctly html entities?
我的示例代码是
<el-input v-model="data" readonly="readonly"></el-input>
我了解v-html
,但是由于自动双向绑定,我更喜欢继续使用v-model
.
I know about v-html
but I prefer keep using v-model
due the automatic two-way binding.
也许我表达了自己的错误,我想显示字符,而不是html实体,所以相反,49.42₹
我需要显示49.42₹.
Maybe I expressed myself wrong, I want to display the character, not the html entity, so instead 49.42₹
i need to display 49.42₹.
推荐答案
如果您对可以解释HTML实体的计算机进行v-建模,那么我认为您将获得想要的效果.您可以输入实体值,它将正确地解释它们.但是,它可能会过早地将变成一个不同的字符;您必须输入#8377;然后返回并插入&.
If you v-model a computed that interprets HTML entities, I think you get the effect you want. You can type in entity values and it will interpret them correctly. However, it might prematurely turn  into a different character; you have to type #8377; and then go back in and insert the &.
new Vue({
el: '#app',
data: {
a: '49.42₹'
},
computed: {
asText: {
get() {
return this.toText(this.a);
},
set(newValue) {
this.a = newValue;
}
}
},
methods: {
toText(html) {
const div = document.createElement('div');
div.innerHTML = html;
return div.textContent;
}
}
})
<link href="//unpkg.com/element-ui@1.0.0-rc.3/lib/theme-default/index.css" rel="stylesheet"/>
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui/lib/index.js"></script>
<div id="app">
<el-input v-model="asText"></el-input>
{{a}}
<div v-html="a"></div>
</div>
这篇关于VueJS V模型自动转义HTML实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!