为什么angular-webpack-stater 的例子在providers 中没有声明AppState,而是声明Title 是aslo service
why the example of angular-webpack-stater don't state AppState in providers,however state Title which is aslo service
代码地址为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
) {}
.....
因为他们在模块级别提供它
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
.
代码地址为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
) {}
.....
因为他们在模块级别提供它
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
.