OK, so I'm new to Vue ( basically, new to JS in general, but I'm playing with Vue right now ) and what I want to do, is to auto hide an element ( not on click ) inside the template tag of a component. In jQuery this would look like:
$(function() {
setTimeout(function() {
$(".hideElement").hide()
}, 1000);
});
but how this works in Vue? my component looks like this:
<template>
<div>
<h1 class="hideElement"> HELLO </h1>
</div>
</template>
<script> // nothing here
</script>
<style> // nothing here
</style>
I know how to toggle the element on click of a button, but I just want to auto hide it after 1 second without any click events everytime the users enter this component ( which is a new "page" )
Select the div element. Use delay function (setTimeOut(), delay()) to provide the delay to hide() the div element.
Feb 3, 2020. The $refs property in Vue is used to reference DOM elements in the Vue instance's templates. A common use case for $refs is focusing on a DOM element when a certain event happens.
Vuex is a state management pattern for vue. js. $t is the injected method from vue. js or Vue. i18n.
$set is "For Vue instance" while Vue. set is "For plain data objects" and that Vue. set is global: However, there are ways to add a property and make it reactive after an instance has been created.
You could just add a property in the data object and use v-show directive to determine whether the element should be visible or not. If the boolean is false the element is hidden, if true the element is visible.
Method Created called synchronously after the instance is created.
<template>
<div>
<h1 v-show="elementVisible" class="hideElement"> HELLO </h1>
</div>
</template>
<script>
export default {
data() {
return {
elementVisible: true
}
},
created() {
setTimeout(() => this.elementVisible = false, 1000)
}
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With