【问题标题】:ui-router annotating resolve with parameters for minifying?ui-router 用参数注释解析以进行缩小?
【发布时间】:2015-06-06 18:01:52
【问题描述】:

如果代码未正确注释,则 ui-router 会在缩小时中断。我已经这样做了,而且我的大部分路线都可以工作,但尤其是这样:

var authRedirect = function(s) {
    return ['$state', '$q', 'AuthService', function($state, $q, AuthService) {
        var d = $q.defer();
        var is_auth = AuthService.checkAuth();
        if(!is_auth) {
            d.reject();
            $state.go(s);
        } else 
           d.resolve();
        return d.promise;
    }];
}

// ...

.state('secret', {
    url: '/secret',
    controller: 'TestCtrl',
    templatesUrl: '/test.html',
    resolve: { authRedirect: authRedirect('other') }
})
.state('other', { .... } )

我将另一个状态传递给我的resolve 函数,如果不满足某些条件,它应该重定向到该状态。我在其他几个路由中使用了这个解析函数,所以它是保持 ui-router 代码干净的一种方便的方法。

虽然非缩小版工作正常,但缩小版不起作用(它什么都不做)。但似乎我已经正确注释了它,因为没有引发错误。有什么问题?

【问题讨论】:

    标签: angularjs angular-ui-router minify


    【解决方案1】:

    请更新代码

    .state('secret', {
        url: '/secret',
        controller: 'TestCtrl',
        templatesUrl: '/test.html',
        resolve: { authRedirect: ['authRedirect', function(authRedirect){ return authRedirect('other')}] }
    })
    

    【讨论】:

    • 嗯,这会破坏整个路由,而不仅仅是重定向部分。
    • @rublex - 我很确定这应该可行。你能分享我更新的代码吗?
    • 我认为你把我的全局函数放在它自己的服务中是对的。我会试试的。但是除了污染全局命名空间之外,authRedirect 作为一个全局函数是不是有什么特别险恶的原因呢?
    • @Rublex - 我的原因与您解释的原因相同。 Angular 制定了一个协议,我们应该这样做。这就是我所相信的。
    【解决方案2】:

    你也可以像这样更新代码试试

    .state('secret', {
        url: '/secret',
        controller: 'TestCtrl',
        templatesUrl: '/test.html',
        resolve: { authRedirect: function() { return authRedirect('other')} }
    })
    

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 2014-12-08
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      相关资源
      最近更新 更多