【问题标题】:Vue - Running a function after another has completedVue - 在另一个功能完成后运行一个功能
【发布时间】:2020-04-07 22:11:53
【问题描述】:

我似乎不知道如何在函数运行后调用函数。

Func1 位于componentA this.$refs.componentA.Func1()

Func2在本地方法this.Func2()中找到

函数完成后如何调用函数。

我已经尝试过这段代码的变体(承诺),但它有一个错误:

this.$refs.componentA
            .Func1()
            .then(response => {
              this.Func2()
            });

得到这些错误:

Error in v-on handler: "TypeError: Cannot read property 'then' of undefined

TypeError: Cannot read property 'then' of undefined

【问题讨论】:

  • 如果Func1不是异步的,就一个接一个地调用,即this.$refs.componentA.Func1(); this.Func2()
  • Hey Phil - Func2() 在 Func1 之后立即运行,即使 Func1 尚未完成
  • 那么Func1 中有什么内容?听起来这是一个糟糕的异步函数

标签: vue.js vuejs2


【解决方案1】:

你可以为 Func1 使用回调函数,也可以使用另一种方法来调用 Promise as 的函数:

Func1(Func2)
Or
Promise.resolve(Func1()).then(Func2)

【讨论】:

    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2016-08-27
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多