Why do I get StaticInjector error if I include a component both in HTML as well as inject it via DI

ε祈祈猫儿з 提交于 2019-12-13 04:18:02

问题


I have a component ValidateSessionComponent which uses LoginFormComponent. I use LoginFormComponent in ValidateSessionComponent by including it in the HTML of the ValidateSessionComponent.

This works fine so far. Then I decided to also include reference of LoginFormComponent via DI in ValidateSessionComponent

constructor(private loginForm2:LoginFormComponent,private helper:HelperService,private dialogService:DialogBoxService,private activatedRoute:ActivatedRoute, private router:Router, private userManagementService:UserManagementService) { }

This started causing the error StaticInjectorError(DynamicTestModule)[ValidateSessionComponent -> LoginFormComponent]: StaticInjectorError(Platform: core)[ValidateSessionComponent -> LoginFormComponent]: NullInjectorError: No provider for LoginFormComponent!

Why do i start getting the error?


回答1:


Because DI works only for providers.

Components aren't providers.

If you wish to get a reference to your child element, use a ViewChild instead.

@ViewChild(LoginFormComponent, { static: true }) loginForm: LoginFormComponent;


来源:https://stackoverflow.com/questions/58265549/why-do-i-get-staticinjector-error-if-i-include-a-component-both-in-html-as-well

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!