我在这两个场景中有点困惑,我们使用@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 ×1