简体   繁体   English

Angular2 Beta依赖注入

[英]Angular2 Beta dependency injection

I have a NavBar Component which loads the QApi Service, the QApi Service loads the UserService, but I get the following error: 我有一个加载QApi服务的NavBar组件,QApi服务加载UserService,但是我收到以下错误:

EXCEPTION: No provider for UserService! (NavBarComponent -> QApi -> UserService)

Either I simply don't get the concept of dependency injection, I made a stupid error, or this is just way to complicated compared to native development... Thanks for your help. 要么我根本没有得到依赖注入的概念,我犯了一个愚蠢的错误,或者这只是复杂的方式与本机开发相比...感谢您的帮助。

Here my code: 这是我的代码:

UserService: UserService:

import {Injectable} from 'angular2/core';
//import {User} from '../data-source-mocks/users';

@Injectable() 
export class UserService {
 public isAuthenticated = true;
}

QApi Service: QApi服务:

import {Injectable} from 'angular2/core';
import {UserService} from '../user/user.service';

@Injectable() 
export class QApi {

constructor(private _userService: UserService) {}

}

NavBar Component: NavBar组件:

import {Component} from 'angular2/core';
import {QApi} from '../../services/q-api/q-api';

@Component({
 selector: 'nav-bar',
 template: `Test NavBar`,
 providers: [QApi]
})

export class NavBarComponent {
private _isAuthenticated = false;

constructor(private _QApi: QApi) {}
}

EDIT: 编辑:

First of all: Thanks for alle the great answers each and every single one helped me to understand dependency injection better, especially this article: https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html 首先:感谢你们给出了很好的答案,每一个人都帮助我更好地理解依赖注入,特别是这篇文章: https//angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html

I changed my QApi class to this: 我将我的QApi课改为:

import {Injectable, Inject, Injector} from 'angular2/core';
import {UserService} from '../user/user.service';
import {CardService} from '../card/card.service';

@Injectable()
export class QApi {

constructor() {
    var _injector = Injector.resolveAndCreate([UserService, 
                                               CardService]);

    this.userService = _injector.get(UserService);
    this.cardService = _injector.get(CardService);
}

}

Now it works like I hoped it would. 现在它的工作方式就像我希望的那样。 Cant thank you guys enough!! 不能谢谢你们!

Add UserService to the component providers : UserService添加到组件providers

@Component({
    selector: 'nav-bar',
    template: `Test NavBar`,
    providers: [QApi, UserService] // <- add UserService here
})
export class NavBarComponent { /* ... */ }

Here are two good articles to better understand Angular2 Dependency Injection: 这里有两篇很好的文章来更好地理解Angular2依赖注入:

In fact both previous responses are true! 事实上以前的两个回答都是真的! ;-) ;-)

You need to define the services: 您需要定义服务:

  • Application level . 申请级别 Within the second parameter of the bootstrap function. bootstrap函数的第二个参数内。 It contains the list of the providers that are available for the whole application. 它包含可用于整个应用程序的提供程序列表。

     bootstrap(App, [UserService, QApi, ...]); 
  • Component level . 组件级别 Within the providers attribute of the Component annotation. Component注释的providers属性中。 In this case, this is only configured for this component and you need to define this for each component where the QApi service. 在这种情况下,仅为此组件配置,您需要为QApi服务的每个组件定义此组件。

     @Component({ selector: 'nav-bar', template: `Test NavBar`, providers: [QApi, UserService] }) 

You also mix things. 你也混合了一些东西。 I mean you can put the UserService provider at the application level and QApi at the component level. 我的意思是你可以将UserService提供者放在应用程序级别,将QApi放在组件级别。 In fact what is important is that Angular can find providers for all the involved elements in the processing chaining (with dependency injection). 事实上,重要的是Angular可以在处理链中找到所有相关元素的提供者(使用依赖注入)。 They can come from either component level (1st) or application level (2nd). 它们可以来自组件级别(第一级)或应用程序级别(第二级)。

Hope that it gives you some additional hints following alexpods and MichaelOryl great answers ;-) 希望它给你一些额外的提示下alexpodsMichaelOryl伟大的答案;-)

Thierry 蒂埃里

List the services in your bootstrap call (wherever you are handling that). 列出引导程序调用中的服务(无论您在何处处理)。 Something like the following should work: 像下面这样的东西应该工作:

bootstrap(App, [UserService, QApi, COMMON_DIRECTIVES, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, HTTP_PROVIDERS]); 
providers// directives added here are available to all children

Then you will have a single instance of each of those services available to the rest of your application. 然后,您将为应用程序的其余部分提供每个服务的单个实例。

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

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