简体   繁体   English

UI-Router的resolve函数参数如何解析?

[英]How are UI-Router's resolve function arguments resolved?

I obviously have missed something from UI-Router and/or angular's documentations so, although I will sound stupid, here it is: 我显然错过了UI-Router和/或angular的文档中的某些内容,因此,尽管我听起来很愚蠢,但这是:

In http://angular-ui.github.io/ui-router/site/#/api/ui.router.state .$stateProvider we have an example resolve function: http://angular-ui.github.io/ui-router/site/#/api/ui.router.state。$ stateProvider中,我们有一个示例resolve函数:

resolve: {
    myResolve1:
      function($http, $stateParams) {
        return $http.get("/api/foos/"+$stateParams.fooID);
      }
    }

I understand that its return value will be injected into the controller under the name "myResolve1". 我知道它的返回值将以名称“ myResolve1”注入到控制器中。

What is less clear to me is where the values for the function parameters "$http" and "$stateParams" are coming from. 我不太清楚函数参数“ $ http”“ $ stateParams”的值来自何处 So, where did the caller find the values to give to this function ? 那么,调用者在哪里找到要提供给该函数的值?

This is a good point, and as discussed for example here 这是一个好点,例如此处讨论的那样

Angularjs ui-router abstract state with resolve AngularJS UI路由器抽象状态与解决

we should use the IoC oriented notation 我们应该使用面向IoC的符号

resolve: {
    dataParent: ['$stateParams', 'ProfileService', function ($stateParams, ProfileService) {
        var username = $stateParams.username;
        return ProfileService.getProfile(username);
    }]
}

The biggest benefit(s) is ... it will work even with minification applied. 最大的好处是...即使缩小也能使用。 But mostly, it is now really clearly stated: 但实际上,现在已经非常清楚地说明了这一点:

there is an array with all required dependency names - and the resolve function as a last argument 有一个带有所有必需依赖项名称的数组-解析函数作为最后一个参数

You can inject anything that you would normally inject into your controller into a resolve function including other resolves. 您可以将通常会注入到控制器中的所有内容注入到包含其他解析的解析函数中。 You need to be careful with this because if you are chaining resolves it will take longer for the state to navigate completely therefore rendering your view slower. 您需要注意这一点,因为如果您要链接解析,则状态完全导航将花费更长的时间,因此会使视图变慢。 An example of chaining resolves would look like this. 链接解析的示例如下所示。

resolve: {
    loggedIn: function(auth){ return  auth.requireLoggedIn()},
    user: function(loggedIn, auth){return auth.resolveUser()}
}

auth is a service that is part of my angular application and as you can see loggedIn is a resolve that needs to resolve before it can be used to load your user. auth是我的角度应用程序中的一项服务,如您所见,loginIn是需要先解析的解决方案,然后才能用于加载您的用户。 You could then inject both of those into your controller. 然后,您可以将它们都注入到控制器中。

You can put any Angular service, factory, filter or resolve in there.. And I'm sure I'm missing some other core things you can add as well but that's generally what I inject into a resolve. 您可以在其中放置任何Angular服务,工厂,过滤器或解决方案。.我敢肯定,我也缺少可以添加的其他一些核心内容,但这通常是我要注入解决方案的内容。 So to answer your question they just come from your Angular application. 因此,要回答您的问题,它们仅来自您的Angular应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM