1

Is it possible to resolve a component from string?

Previously in Angular 2, we are able to do this see here. However, in Angular 4, the same method will throw error.

private resolver: ComponentFactoryResolver

var factories = Array.from(this.resolver['_factories'].keys());
var factoryClass = <Type<any>>factories.find((x: any) => x.name === this.comp);
const factory = this.resolver.resolveComponentFactory(factoryClass);
const compRef = this.vcRef.createComponent(factory);
2
  • what is the error? Commented Jan 17, 2018 at 11:40
  • Element implicitly has an 'any' type because 'ComponentFactoryResolver' has no index signature Commented Jan 17, 2018 at 11:57

1 Answer 1

2

It looks like you might be relying on an implementation detail. However, to get around the error, you can explicitly cast the type to any in order to access the indexer. Assuming there is a _factories property, this should work:

var factories = Array.from((<any>this.resolver)['_factories'].keys());
Sign up to request clarification or add additional context in comments.

1 Comment

it's not works on angular 11.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.