我试图在表单文本输入中显示一些 html 实体,但 v-model
似乎转义了它们。
我需要写一些东西来使v-model
正确显示html实体吗?
我的示例代码是
<el-input v-model="data" readonly="readonly"></el-input>
我了解 v-html
,但由于自动双向绑定(bind),我更喜欢继续使用 v-model
。
更新
也许我表达错了,我想显示字符,而不是html实体,所以我需要显示49.42£。
最佳答案
如果你对解释 HTML 实体的计算模型进行 v 建模,我认为你会得到你想要的效果。您可以输入实体值,它会正确解释它们。然而,它可能会过早地将 变成不同的字符;你必须输入#8377;然后返回并插入 &。
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/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="61040d040c040f154c140821504f514f514c13024f52" rel="noreferrer noopener nofollow">[email protected]</a>/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>
关于forms - Vuejs v-model 自动转义 html 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53282349/