简体   繁体   English

Angular2中的依赖注入-TypeScript语法与@Inject

[英]Dependency Injection in Angular2 - TypeScript syntax vs @Inject

I'm using Angular2 build 2.0.0-alpha.34 and I'm not sure why these two different pieces of code are giving me different results. 我正在使用Angular2 build 2.0.0-alpha.34 ,我不确定为什么这两个不同的代码会给我不同的结果。

The only difference is using @Inject(TitleService) titleService vs using titleService: TitleService 唯一的区别是使用@Inject(TitleService) titleService与使用titleService: TitleService

This correctly works (ES2015 way) 这可以正常工作(ES2015方式)

import {Inject, Component, View, bootstrap, NgFor} from 'angular2/angular2';
import {titleComponent} from './components/title';
import {TitleService} from './services/services';

// Annotation section
@Component({
    selector: 'my-app'
})
@View({
    templateUrl: './html/app.html',
    directives: [titleComponent, NgFor]
})
// Component controller
class MyAppComponent {
    titles: Array<Object>;
    constructor(@Inject(TitleService) titleService) {
        console.log(titleService)
        this.titles = titleService.getTitles();
    }
}

bootstrap(MyAppComponent,[TitleService]); 

This doesn't work (TypeScript way), it doesn't ever make it to the console.log call in constructor, but it doesn't throw an error 这是行不通的(以TypeScript方式进行),也从未在构造函数中的console.log调用中使用,但不会引发错误

import {Inject, Component, View, bootstrap, NgFor} from 'angular2/angular2';
import {titleComponent} from './components/title';
import {TitleService} from './services/services';

// Annotation section
@Component({
    selector: 'my-app'
})
@View({
    templateUrl: './html/app.html',
    directives: [titleComponent, NgFor]
})
// Component controller
class MyAppComponent {
    titles: Array<Object>;
    constructor(titleService: TitleService) {
        console.log(titleService)
        this.titles = titleService.getTitles();
    }
}

bootstrap(MyAppComponent,[TitleService]); 

If I'm using TypeScript's way to inject, do I need to do something else somewhere? 如果我使用TypeScript的注入方式,是否需要在其他地方进行其他操作?

You need to add it in the viewBindings of your Component annotation : 您需要将其添加到Component批注的viewBindings中:

viewBindings: [TitleService]

Note that you only need to do it once, say in the Root component and every other component within the hierarchy will get it injecting from the root component, as if the component's injector can't resolve the dependency it goes up until it finds it. 请注意,您只需要执行一次,例如在Root组件中,层次结构中的所有其他组件都可以从根组件中注入它,就好像该组件的注入器无法解析它找到之前的依赖关系一样。 If you define one viewBindings you should have the same instance everywhere I believe. 如果您定义一个viewBindings,那么我相信每个地方都应该有相同的实例。

edit: renaming of parameter in alpha 34. 编辑:重命名alpha 34中的参数。

I had TitleService defined in a JavaScript file, using an ES2015 class, I just needed to change it to a TypeScript file so it compiled correctly. 我使用ES2015类在JavaScript文件中定义了TitleService,我只需要将其更改为TypeScript文件即可正确编译。 Oops. 哎呀。

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

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