Introspecting the Decorated Class
Explore how to introspect classes decorated with Angular-style decorators by accessing metadata using Reflect methods. Understand how Babel transpiles decorator syntax for Node.js compatibility and learn to verify metadata injection in classes. Gain foundational insights into dynamic class behavior modification through decorators.
We'll cover the following...
Once a class is decorated with Angular decorators like @Component, @Pipe, @Directive, and so on, Angular can read the metadata to figure out what to do with them.
In our example, where we simulated the Angular @Component decorator, we stored the properties given to the decorator into the metadata.
Verification by example
Let’s verify that the decoration actually worked.
{
"presets": ["es2016"],
"plugins": [
"transform-decorators-legacy"
]
}
Explanation
-
We import
SampleComponentand query for its metadata keys by callingReflect.getMetadataKeys(); this method is added to theReflectclass by thereflect-metadatalibrary that we imported earlier. -
Then we obtain the metadata for the
annotationsproperty using theReflect.getMetadata()method.
Files in src folder
Let’s summarize the effect of all the decorator-related ...