Хотя ваша основная проблема была решена, вы можете упростить свой код и в то же время устранить потребность в jQuery, выполнив его способом Vue.
Используя привязки классов , вы можетезамените свои if на вычисленную реквизит, и если вам нужен прямой доступ к элементу, вы можете использовать refs .
. Просто запустите следующий фрагмент, чтобы увидеть эти концепции в действии:
new Vue({
el: '#app',
data: {
fieldContent: '',
fieldLabel: '',
active: false,
},
computed: {
classList() {
return {
'form-field--is-filled': this.fieldContent !== '',
'form-field--is-active': this.active,
};
}
},
methods: {
setActive(active, event) {
console.log("this.fieldContent: "+this.fieldContent);
this.active = active;
}
},
updated() {
console.log("in initialize form field");
console.log("this.fieldContent: "+this.fieldContent);
console.log("result from shorthand getElementById: ", this.$refs.outerDiv);
console.log("classList of element: "+ this.$refs.outerDiv.classList);
}
});
.form-control{
border-style: none;
width: 100%;
height: 34px;
padding: 0px 0px;
font-size: 20px;
}
.form-field--is-active {
border: 1px solid green;
}
.form-field--is-filled {
outline: 1px dotted blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div class="form-field" id="outerdiv" :class="classList" ref="outerDiv">
<div class="form-field__control">
<label for="exampleField" class="form-field__label">{{fieldLabel}}</label>
<input id="exampleField" v-model="fieldContent" @blur="setActive(false, $event)" @focus="setActive(true, $event)"
type="text" class="form-field__input" />
</div>
</div>
</div>