1 秒后自动隐藏 VueJS 中的元素

autohide an element in VueJS after 1 second

好的,所以我是 Vue 的新手(基本上,一般来说是 JS 的新手,但我现在正在玩 Vue)我想做的是自动隐藏一个元素(不是点击) 在组件的模板标签内。在 jQuery 这看起来像:

$(function() {
    setTimeout(function() {
        $(".hideElement").hide()
    }, 1000);
});

但这在 Vue 中如何工作?我的组件如下所示:

<template>
<div>
 <h1 class="hideElement"> HELLO </h1>
</div>
</template>

<script> // nothing here 
</script>

<style> // nothing here
</style>

我知道如何在点击按钮时切换元素,但我只想在用户每次输入此组件(这是一个新的 "page" )时在没有任何点击事件的情况下在 1 秒后自动隐藏它

您可以只在数据对象中添加一个 属性 并使用 v-show 指令来确定该元素是否应该可见。如果布尔值为 false,则元素隐藏,如果为 true,则元素可见。

Method 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>