我有一个奇怪的恼人错误。我有一个ul
,它会显示一个数组是否有值,所以我使用v-if
。当数组有值时,我想对它应用一些css。但是因为v-if
和实际存在的元素之间存在延迟,我的选择器找不到该元素。
<template>
<ul v-if="errors.length" class="error">
<li v-for="error in errors">{{ error }}</li>
</ul>
</template>
<script type="text/javascript">
export default {
data() {
return {
errors: [],
}
}
methods: {
validate() {
if (someErrorCondition) {
this.errors.push('You have an error');
// The next call fails because .error doesn't exist yet
$(this.$el).find('.error').css('border', 'solid 10px red');
// Error above; $(this.$el).find('.error') is not found. Same for $('.error')
// This is because of the v-if
// NOTE: the second time validate is called, this works, presumably because it exists now.
}
}
}
}
</script>