According to the Angular documentation for the @ViewChild decorator, one of the supported selectors is a provider with a string token. However when I define a provider using a string token in a child component and try and reference it using the @ViewChild decorator the view query is undefined.
I know it's possible to reference the provider using the read property with the component it's registered in as a selector, but how do I reference it using the string token as a selector alone?
I'm specifically talking about this type of string token selector (not a class provider, template reference variable, component, directive or a TemplateRef):
Any provider defined through a string token (e.g.
@ViewChild('someToken') someTokenVal: any)
Here's an example:
@Component({
selector: 'app-child',
template: `<h1>Child</h1>`,
providers: [{ provide: 'Token', useValue: 'Value' }]
})
export class ChildComponent{}
@Component({
selector: 'app-parent',
template: `<app-child></app-child>`
})
export class ParentComponent implements AfterViewInit{
@ViewChild('Token') childToken: string;
ngAfterViewinit(){
console.log('token: ', childToken);
}
}
This logs:
token: undefined