Angular: 如何使用 appInject 将服务注入组件?

Angular: How do I inject a service into a component using appInject?

我正在关注 google 的 angular2 quick start(这些示例没有 Github?!)

在注入服务之前一切都很顺利。

这是我的代码:

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

class FriendsService {
    names: Array<string>;

    constructor(){
        this.names = ['Aviad', 'Chen', 'Yarden'];
    }
}

@Component({
    selector: 'display',
    appInjector: [FriendsService]
})
@View({
    template:
    <p>My name: {{ myName }}</p>
    <p>Friends:<p>
    <ul>
        <li *ng-for="#name of names">
            {{name}}
        </li>
    </ul>
    ,
    directives: [NgFor]
})

export default class DisplayComponent {
    myName: string;
    names: Array<string>;

    constructor(friendsService: FriendsService){
        this.myName = 'Alice';
        this.names = friendsService.names;
    }
}

我 运行 有很多例外,但第一个也是我认为最相关的是:

EXCEPTION: No provider for FriendsService! (DisplayComponent -> FriendsService) angular2.dev.js:22367 STACKTRACE: angular2.dev.js:22367 Error: DI Exception

    at NoBindingError.BaseException (angular2.dev.js:7735)
    at NoBindingError.AbstractBindingError (angular2.dev.js:9029)
    at new NoBindingError (angular2.dev.js:9052)
    at Injector.execute._proto._throwOrNull (angular2.dev.js:27552)
    at Injector.execute._proto._getByKeyDefault (angular2.dev.js:27597)
    at Injector.execute._proto._getByKey (angular2.dev.js:27545)
    at Injector.execute._proto._getByDependency (angular2.dev.js:27533)
    at Injector.execute._proto._instantiate (angular2.dev.js:27430)
    at Injector.execute._proto._new (angular2.dev.js:27403)
    at InjectorInlineStrategy.execute.protoStrategy.instantiateBinding (angular2.dev.js:27192) angular2.dev.js:22367 ERROR CONTEXT:

可能相关的一件事是 <display> 组件是 <my-app> 组件的指令:

import {Component, View, bootstrap} from 'angular2/angular2';
import DisplayComponent from './show-properties';

@Component({
    selector: 'my-app'
})
@View({
    template: '<display>',
    directives: [DisplayComponent]
})
class AppComponent { }

bootstrap(AppComponent);

这成功了:

@Component({
    selector: 'display',
    bindings: [FriendsService] // instead of appInjector
})

当其他都和上面一样的时候。

反正按照changelog this was a breaking change (thanks jesse Good):

BREAKING CHANGES

THe appInjector property has been removed. Instead use viewInjector or hostInjector.

I guess that the Angular2 Quick Start guide is not as up to date as I thought.