相关疑难解决方法(0)

@Inject与构造函数注入作为Angular 2中的普通参数有什么区别?

我在这两个场景中有点困惑,我们使用@Injectable()装饰器标记一些类,以便它们可用于注入不同的组件.我只是想知道@Inject()构造函数注入之间的区别是正常的.

场景1 - 使用@Inject():

@Component({
    selector: 'main-app',
    template: `
        ....
        {{_service.getName()}}
        ....
    `
})
export class AppComponent{
    constructor(@Inject(AppService) private _service){}
    ....
}
Run Code Online (Sandbox Code Playgroud)

场景2 - 用作Normal参数:

@Component({
    selector: 'main-app',
    template: `
        ....
        {{_service.getName()}}
    `
})
export class AppComponent{
    constructor(private _service:AppService){}
    ....
}
Run Code Online (Sandbox Code Playgroud)

两种情况都有效,有什么区别吗?谁应该更优选?

angular

5
推荐指数
1
解决办法
1839
查看次数

标签 统计

angular ×1