我有一个输入字段,我可以在其中写入一些内容,现在如果我按输入,一个名为“gespeichert”的值就会变为 true。我还有一个与 v-if 绑定(bind)的 p 标签。现在我希望它在 2 或 3 秒后隐藏/删除它。 这是用 vue.js 制作的
我已经尝试过
methods: {
speichern: function() {
this.gespeichert = true;
setTimeout(function(){
this.gespeichert = false;
}, 2000);
....
现在我希望 gespeichert 在 2 秒后得到值 false,为什么这不起作用?
最佳答案
您遇到了作用域问题 - setTimeout 函数内的 this
与该函数之外的内容不同。您可以使用 .bind(this)
来解决这个问题:
setTimeout(function(){
this.gespeichert = false;
}.bind(this), 2000);
关于javascript - 我可以使用 vue js 显示 <p> 标签,但 2 秒后无法删除它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54679799/