【问题标题】:i can display an <p> tag with vue js but i cant remove it after 2 sec [duplicate]我可以用vue js显示一个<p>标签,但我不能在2秒后删除它[重复]
【发布时间】:2019-07-07 20:29:43
【问题描述】:

我有一个输入字段,我可以在其中写一些东西,现在如果我按下输入一个名为“gespeichert”的值就会变为真。我还有一个与 v-if 绑定的 p 标签。现在我希望它在 2 或 3 秒后隐藏/删除它。 这是用 vue.js 制作的

我已经试过了

                    methods: {

        speichern: function() {
            this.gespeichert = true;
            setTimeout(function(){

                    this.gespeichert = false;

            }, 2000);
                             ....

现在我希望 gespeichert 在 2 秒后获得 false 值,为什么这不起作用?

【问题讨论】:

  • 使用 setTimeout(()=&gt;{ 这样的箭头函数来访问它

标签: javascript html vue.js outerhtml


【解决方案1】:

您有一个范围问题 - 您的 setTimeout 函数中的 this 不是该函数之外的内容。你可以使用.bind(this)来解决这个问题:

setTimeout(function(){
    this.gespeichert = false;
}.bind(this), 2000);

【讨论】:

  • 完美!谢谢你的作品正是我想要的:)
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 2017-03-16
  • 2022-01-05
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多