是什么使用之间的差(@注入(HTTP)的http:HTTP)或不 [英] what is the difference between using (@Inject(Http) http: Http) or not

查看:129
本文介绍了是什么使用之间的差(@注入(HTTP)的http:HTTP)或不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此<一个href=\"http://stackoverflow.com/questions/35350130/error-using-http-json-angularjs-2/35350172?noredirect=1#35350547\">question,我有新的问题。这两种方式之间的区别是什么?

After this question, I have new a question. What is the difference between these two ways?

这是我最初的code:

This was my initial code:

import {Http, HTTP_PROVIDERS} from 'angular2/http';

@Component({

viewProviders: [HTTP_PROVIDERS],
..//

constructor(http: Http){
..//

但在做测试时,他们提供了codeS查找错误:

but while doing tests, They provided with codes to find the error:

import { Component, Inject} from 'angular2/core';
//above code should be at start.

constructor(@Inject(Http) http: Http) {

我觉得它的工作原理相同无论哪种方式,有人能告诉我,有什么区别,或者只是第一种是像第二个,但 @Inject 是隐性或一些similar.Sorry我的英语水平。

I think it works the same either way, someone could tell me, what is the difference, or just the first one is like the second but @Inject is implicit or something similar.Sorry for my English.

推荐答案

其实, @Inject 装饰器要求某种类型的依赖。字符串还支持在这个水平。

In fact, @Inject decorator is used to ask for a dependency of a certain type. A string is also supported at this level.

如果你已经有了类的如组件注射一个装饰,并指定在打字稿一个类型参数(你的情况),使用这种装饰等是没有必要的,因为该决议将自动按类。如果你不提供一个类型, @Inject 装饰将是有益的...

If you already have a decorator of the class like Component or Injectable and you specify a type in TypeScript for the parameter (your case), using this other decorator isn't necessary because the resolution will be by class automatically. If you don't provide a type, the @Inject decorator will be useful...

另一个用例。如果你想使用 ngUpgrade (混合Angular1 / Angular2应用),Angular1的工厂只有名字登记的,你不能按类解决这些问题。因此,使用 @Inject 必要在这里。请参阅本plunkr: http://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg?p=$p $ PVIEW 。这是写在打字稿,但没有标注,但可以适应;)

Another use case. If you want to use ngUpgrade (hybrid Angular1 / Angular2 applications), factories of Angular1 are registered by name only and you can't resolve them by class. So using @Inject is necessary here. See this plunkr: http://plnkr.co/edit/yMjghOFhFWuY8G1fVIEg?p=preview. It's written in TypeScript but without annotation but can be adapted ;)

您也可以看看这个链接:

You could also have a look at this link:

这篇关于是什么使用之间的差(@注入(HTTP)的http:HTTP)或不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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