Angular ui-router 中的 Promise 依赖解析顺序

Promise dependency resolution order in angular ui-router(Angular ui-router 中的 Promise 依赖解析顺序)
本文介绍了Angular ui-router 中的 Promise 依赖解析顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个顶级控制器,它仅在成功解决承诺(由 Config 工厂返回)时实例化.该承诺基本上是下载 Web 应用配置,带有 RESTful 端点等.

I have set up a top-level controller that is instantiated only when a promise (returned by a Config factory) is successfully resolved. That promise basically downloads the Web app configuration, with RESTful endpoints and so on.

$stateProvider
  .state('app', {
    url: '/',
    templateUrl: 'views/_index.html',
    controller: 'MainCtrl',
    resolve: {
      config: 'Config'
    }
  });

此设置允许我在任何较低的控制器有机会使用它之前断言配置已正确加载.

This setup allows me to kind-of assert that the configuration is properly loaded before any lower controller gets a chance to use it.

现在我需要在更深的嵌套控制器中注入另一个 factory,它使用 Config 并且仅在它被解析时才起作用(看它像 $resource 包装器,需要一些 Web 服务 URL).如果我这样做:

Now I need to inject, in a deeper nested controller, another factory that uses Config and only works when it is resolved (look at it like a $resource wrapper that needs some Web service URLs). If I do:

$stateProvider
  .state('app.bottom.page', {
    url: '/bottom/page',
    templateUrl: 'views/_a_view.html',
    controller: 'BottomLevelCtrl',
    resolve: {
      TheResource: 'MyConfigDependingResource'
    }
  });

看起来 resolve 评估顺序不是从上到下,而是从下到上,因此:

it looks like the resolve evaluation order does not follow the controller hierarchy from top to bottom, but from bottom to top, therefore:

  1. app.bottom.page 已输入
  2. ui-router尝试解析MyConfigDependingResource,但是注入失败,因为 Config 从未被初始化过
  3. ui-router 解析因错误而停止(甚至没有抛出 Error,但这是另一个问题),并且 Config 是从未被顶级控制器初始化
  1. app.bottom.page is entered
  2. ui-router attempts to resolve MyConfigDependingResource, but the injection fails, because Config has never been initialized
  3. The ui-router resolution stops because of an error (without even throwing Errors, but that's another issue), and Config is never initialized by the top level controller

为什么 ui-router 以相反的顺序解析依赖关系?如何轻松解决我的 TheResource 对象 顶级 MainCtrl 已解决 Config (不依赖 $inject,当然)?

Why is ui-router resolving dependencies in a reverse order? How can I easily resolve my TheResource object after the top level MainCtrl has resolved Config (without relying on $inject, of course)?

更新:从 这个 plnkr 的日志你可以看到只有在嵌套控制器开始自己的解析过程后才会尝试顶级 resolve.

UPDATE: from this plnkr's log you can see that the top level resolve is attempted only after the nested controller has started its own resolving process.

推荐答案

类似于@Kasper Lewau 的回答,可以指定对单个状态的解析的依赖.如果您的一个解析依赖于同一解析块中的一个或多个解析属性.在我的情况下 checkS 依赖于另外两个解决方案

Similarly to @Kasper Lewau's answer, one may specify a dependency on resolves withing a single state. If one of your resolves depends on one or more resolve properties from the same resolve block. In my case checkS relies on two other resolves

.state('stateofstate', {
    url: "/anrapasd",
    templateUrl: "views/anrapasd.html",
    controller: 'SteofsteCtrl',
    resolve: {
        currU: function(gamMag) {
            return gamMag.checkWifi("jabadabadu")
        },
        userC: function(gamUser, $stateParams) {
            return gamUser.getBipi("oink")
        },
        checkS: ['currU', 'userC', 'gamMag', function(currU, userC, gamMag) {
            return gamMag.check(currU, userC);
        }]
    }
})

<小时>

**PS:**检查以下文档 了解更多关于 resolve 的内部工作原理.


**PS: **Check the "Resolves" section of the following document for more details about the inner-workings of resolve.

这篇关于Angular ui-router 中的 Promise 依赖解析顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)