问题
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