Angularjs:在app.config中,这样来防止微小注入服务 [英] Angularjs: Inject Service in app.config such that to safeguard against minification

查看:104
本文介绍了Angularjs:在app.config中,这样来防止微小注入服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在注入服务所示的app.config中的app.config注入的服务。然而,微小打破了应用程序。如何克服呢?

这不缩小工作:

 的app.config(['$ routeProvider',函数($ routeProvider){
    $ routeProvider
        。什么时候('/',
        {
            templateUrl:谐音/ editor.html
            控制器:AppCtrl
            解析:{
                       //以下方法不能与缩小工作
                        数据:的getData
                     }
        })
     函数的getData(dbService){
         返回dbService.getData();
     }
}]);

请注意以下code不起作用:(打字稿不允许编译)

  ['dbService',函数的getData(dbService){
               返回dbService.getData();
}]


解决方案

为了以防止微小,你需要注释(请参见依赖注释的这里)像你这样的数据做了功能与配置功能。

有两种方法可以做到这一点。

1

不是传递函数,传递一个数组的依赖关系的名称和功能

 的app.config(['$ routeProvider',函数($ routeProvider){
$ routeProvider
    。什么时候('/',
    {
        templateUrl:谐音/ editor.html
        控制器:AppCtrl
        解析:{
            //注释这对缩小prevent
            数据:['dbService'的getData]
        }
    })    函数的getData(dbService){
        返回dbService.getData();
    }
}]);

2

添加您的依赖于$注入性上的功能

 的app.config(['$ routeProvider',函数($ routeProvider){
    $ routeProvider
        。什么时候('/',
        {
            templateUrl:谐音/ editor.html
            控制器:AppCtrl
            解析:{
                //下面的功能注释
                数据:的getData
            }
        })    //注释与$这个功能注入微小注射后使适当的依赖关系
    。$的getData注入= ['dbService'];
    函数的getData(dbService){
        返回dbService.getData();
    }
}]);

I am trying to inject a service in app.config as illustrated in Inject service in app.config. However, minification breaks the app. How to overcome this?

This doesn't work with minification:

app.config(['$routeProvider',function ($routeProvider) {
    $routeProvider
        .when('/',
        {
            templateUrl: "partials/editor.html",
            controller: "AppCtrl",
            resolve: {
                       //Following method doesn't work with minification
                        data: getData
                     }
        })
     function getData (dbService) {
         return dbService.getData();
     }
}]);

Please note the following code doesn't work: (Typescript does not allow compilation)

['dbService',function getData(dbService){
               return dbService.getData();
}]

解决方案

In order to safeguard against minification, you need to annotate (see Dependency Annotation here) the data function like you did with the config function.

There are two ways to do this.

1.

Instead of passing a function, pass an array with the names of the dependencies and the function

app.config(['$routeProvider',function ($routeProvider) {
$routeProvider
    .when('/',
    {
        templateUrl: "partials/editor.html",
        controller: "AppCtrl",
        resolve: {
            //annotate this to prevent against minification
            data: ['dbService', getData]
        }
    })

    function getData (dbService) {
        return dbService.getData();
    }
}]);

2.

Add your dependencies to a $inject property on your function

app.config(['$routeProvider',function ($routeProvider) {
    $routeProvider
        .when('/',
        {
            templateUrl: "partials/editor.html",
            controller: "AppCtrl",
            resolve: {
                //function annotated below
                data: getData
            }
        })

    //annotate this function with $inject so proper dependencies injected after minification
    getData.$inject = ['dbService'];
    function getData (dbService) {
        return dbService.getData();
    }
}]);

这篇关于Angularjs:在app.config中,这样来防止微小注入服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆