在创建方法之前使用 async 是否会破坏 vue 的生命周期

发布于 2025-01-20 12:36:38 字数 168 浏览 3 评论 0原文

我已经读到,我可以在创建方法中添加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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2025-01-27 12:36:38

通过使用async它已转换为异步动作,并在后台发送到队列

。它不在后台运行。它甚至没有推迟到“以后”,功能主体立即开始执行。它可以等待的唯一区别,在这一点上,async函数在“某物”完成后,返回其最终结果的承诺。但是Vue忽略了回归的承诺(就像任何退货值一样)。

因此,是的,代码 等待可能会运行“订单”在那时的前景中。换句话说,如果已安装依赖于创建的的某些东西,则需要同步完成。您仍然可以在async函数的第一部分中执行此操作。

这与在没有async的情况下定义的创建方法中创建前景链没有什么不同。

By using async it's converted into asynchronous action and being send to the queue in the background

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 the async 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 like mounted was called, but it'll still run in the foreground at that point. In other words, if mounted relies on some stuff from created, that will need to be done synchronously. You can still do that in the first part of an async function though.

This is not any different from creating a promise chain in a created method that is defined without async.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文