As stated in the Vue documentation regarding v-model
:
By default, v-model
on a component uses value
as the prop and input
as the event.
As you need an specif behavior, you'll need to have methods to decode (when showing) and to encode (when updating) the value, separating the v-model
into :value
and @input
.
So, your next problem will be just how to decode and encode HTML entities with JavaScript. There are some ways already discussed many1 times2 here3. I like the mathiasbynens/he library to do it, so here's a sample code showing it in action along with Vue:
new Vue({
el: '#app',
data () {
return {
name: 'Name‽'
}
},
methods: {
encode (value) {
this.name = he.encode(value)
},
decode (value) {
return he.decode(value)
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/he@1.2.0/he.min.js"></script>
<div id="app">
<input :value="decode(name)" @input="encode($event.target.value)" />
<p>{{ name }}</p>
</div>