具有依赖项注入和最小化的打字稿中的Angular服务 [英] Angular service in typescript with dependency injection and minification
问题描述
此刻,我正尽我所能.我目前正在查看服务,我也在使用打字稿作为代码.
I am trying to get my head round angularjs at the moment. I am currently looking at services I am also using typescript for code.
现在从网络上的示例中,我已经看到人们在打字稿中使用如下所示的服务.
Now from samples on the web I have seen that people use something like below for a service in typescript.
class Service
{
constructor( private $http: ng.IHttpService )
{
}
public MyMethod()
{
this.$http.get( "/" )
.success( null )
.error( null );
}
}
现在,如果将其缩小,我会从构造函数中丢失$http
,而angular需要变量名称.因此,我检查了一下,发现可以使用$ inject代替构造函数,但这也会遇到相同的缩小问题.
Now if this is minified I would lose $http
from the constructor and angular requires the variable names. So I checked around and found I can use $inject instead of the constructor but this also would get the same minification problem.
人们在打字稿背景下如何处理缩小和角度化问题?我正在努力寻找一些有关如何处理该问题的可靠文档.在我看来,在现代api中出现这些问题似乎很奇怪,因此我必须在某处缺少某些内容.
How are people dealing with minification and angular in a typescript context? I am struggling to find some solid docs on how this should be handled. To me this seems odd to have these problems in a modern api so I must be missing something somewhere.
推荐答案
只需使用$inject
语法即可.例如:
Just using the $inject
syntax. e.g. :
class Service
{
static $inject = ['$http'];
constructor( private $http: ng.IHttpService )
{
}
public MyMethod()
{
this.$http.get( "/" )
.success( null )
.error( null );
}
}
PS:我对此主题做了详细的视频: http://www .youtube.com/watch?v = WdtVn_8K17E& hd = 1
PS: I did a detailed video on the subject : http://www.youtube.com/watch?v=WdtVn_8K17E&hd=1
这篇关于具有依赖项注入和最小化的打字稿中的Angular服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!