简体   繁体   English

依赖注入Angular 2

[英]Dependency injection Angular 2

I'm trying to inject a service to my component but I keep getting an error. 我正在尝试向我的组件注入服务,但我一直收到错误。

This is my component: 这是我的组成部分:

import {Component, View} from 'angular2/angular2';
import {DisplayService} from '../../services/DisplayService';

@Component({
  selector: 'display'
})
@View({
  templateUrl: './components/display/display.html',
  styleUrls: ['./components/display/display.css']
})
export class Display {
  displays: Array<any>;

  constructor(public displayService: DisplayService){

  }
}

This is my service: 这是我的服务:

import {HTTP_BINDINGS, Http} from 'http/http';

export class DisplayService {
    displays: Array<any>;

    constructor(public http: Http){

    }

    getDisplays() {
        var path = 'http://localhost:8000/get';
        this.http.get(path)
    }
}

And this is my main component: 这是我的主要组成部分:

import {Component, View, bootstrap} from 'angular2/angular2';
import {HTTP_BINDINGS, Http} from 'angular2/http';

import {DisplayService} from './services/DisplayService';

@Component({
  selector: 'app',
  viewBindings: [DisplayService]
})
@View({
  templateUrl: './app.html',
  styleUrls: ['./app.css']
})
class App {}

bootstrap(App, [ROUTER_BINDINGS, HTTP_BINDINGS]);

EXCEPTION: Error during instantiation of Token(Promise<ComponentRef>)!.

ORIGINAL EXCEPTION: Cannot resolve all parameters for DisplayService(?). Make sure they all have valid type or annotations.

I tried passing in the Http package when I'm injecting the service into the component but also just threw an error. 当我将服务注入组件时,我尝试传入Http包,但也只是抛出错误。

After reading the articles provided in the comments by @EricMartinez I realized what was wrong since the service class is so barebone it doesn't produce the meta data needed for the di framework to work. 在阅读了@EricMartinez评论中提供的文章之后,我意识到错误,因为服务类是如此的准确,它不会产生di框架工作所需的元数据。 So here is how my code looks like now: 所以这就是我的代码现在的样子:

Component: 零件:

import {Component, View, NgFor} from 'angular2/angular2';

import {Subbar} from '../subbar/subbar';
import {DisplayCard} from '../display-card/display-card';

import {DisplayService} from '../../services/display_service';

@Component({
  selector: 'display'
})
@View({
  templateUrl: './components/display/display.html',
  styleUrls: ['./components/display/display.css'],
  directives: [Subbar, DisplayCard, NgFor]
})
export class Display {
    displays: Array<any>;

    constructor(public displayService: DisplayService){
        displayService.getDisplays()
            .toRx()
            .map(res => res.json())
            .subscribe(displays => this.displays = displays.screens);
    }
}

Service: 服务:

import {Http} from 'angular2/http';
import {Injectable} from 'angular2/di';

@Injectable()
export class DisplayService {
    constructor(public http: Http){

    }

    getDisplays() {
        var path = 'http://localhost:8000/getscreens';
        return this.http.get(path);
    }
}

As you can see adding the @Injectable() forces the meta data generation. 正如您所看到的,添加@Injectable()会强制生成元数据。 Another option to set up your service is to do this: 设置服务的另一个选择是:

import {Http} from 'angular2/http';
import {Inject} from 'angular2/di';

export class DisplayService {
    constructor(@Inject(Http) http: Http){

    }

    getDisplays() {
        var path = 'http://localhost:8000/getscreens';
        return this.http.get(path);
    }
}

Its up to taste I just found the first way to do it cleaner. 它的味道我刚刚找到了第一种清洁方法。

You have to import 你必须导入

import {Injectable} from 'angular2/core';

into your service and put @Injectable annotation at the top of DisplayService. 进入您的服务并将@Injectable注释放在DisplayService的顶部。

The last thing is to put DisplayService into bootstrap binding parameter. 最后一件事是将DisplayService放入bootstrap绑定参数。

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

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