1

代码地址为https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/home/home.component.ts

import { AppState } from '../app.service';
import { Title } from './title';
import { XLargeDirective } from './x-large';

@Component({

  selector: 'home',

  providers: [
    Title
  ],

  styleUrls: [ './home.component.css' ],

  templateUrl: './home.component.html'
})
export class HomeComponent implements OnInit {

  public localState = { value: '' };

  constructor(
    public appState: AppState,
    public title: Title
  ) {}
.....
4

1 回答 1

1

因为他们在模块级别提供它

https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/app.module.ts#L38

https://github.com/AngularClass/angular2-webpack-starter/blob/master/src/app/app.module.ts#L67

Angulars DI 是分层的。当组件具有依赖项时,DI 查看组件提供者,它是父组件提供者AppComponent,然后继续在模块级别提供的提供者处查找AppState.

于 2017-02-15T10:24:39.943 回答