在创建方法之前使用 async 是否会破坏 vue 的生命周期
我已经读到,我可以在创建
方法中添加async
在VUE JS中 但这并没有打破VUE生命周期,因为通过使用异步,它已将其转换为异步动作并在后台发送到队列,并且可以在之前执行以下方法,例如
方法完成了它的工作已安装
创建的
I've read that I can add async
before created
method in Vue js
but doesn't that break the vue life cycle because by using async it's converted into asynchronous action and being send to the queue in the background and by that it can execute the later method like mounted
before created
method finishes it's job
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。它不在后台运行。它甚至没有推迟到“以后”,功能主体立即开始执行。它可以
等待
的唯一区别,在这一点上,async函数
在“某物”完成后,返回其最终结果的承诺。但是Vue忽略了回归的承诺(就像任何退货值一样)。因此,是的,代码
等待
可能会运行“订单”在那时的前景中。换句话说,如果已安装
依赖于创建的
的某些东西,则需要同步完成。您仍然可以在async
函数的第一部分中执行此操作。这与在没有
async
的情况下定义的创建
方法中创建前景链没有什么不同。Nope. It does not run in the background. It's not even deferred to "later", the function body starts executing immediately. The only difference that it can
await
something, at which point theasync function
returns a promise for its eventual result, after the "something" has finished. But Vue ignores that returned promise (like any return value).So yes, code after the
await
might run "out of order", after another hook likemounted
was called, but it'll still run in the foreground at that point. In other words, ifmounted
relies on some stuff fromcreated
, that will need to be done synchronously. You can still do that in the first part of anasync
function though.This is not any different from creating a promise chain in a
created
method that is defined withoutasync
.