Skip to content

Instantly share code, notes, and snippets.

@NathanWalker
Created August 30, 2016 16:04
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanWalker/36930bba00ceac289186843da50c33e2 to your computer and use it in GitHub Desktop.
Save NathanWalker/36930bba00ceac289186843da50c33e2 to your computer and use it in GitHub Desktop.
An Angular 2 custom Component decorator which sets ViewEncapsulation.None to all components that use it.
// angular
import {Component, ViewEncapsulation} from '@angular/core';
declare var Reflect: any;
const _reflect: any = Reflect;
// Usage:
// @BaseComponent({ etc... })
export function BaseComponent(metadata: any = {}) {
return function(cls: any) {
let annotations = _reflect.getMetadata('annotations', cls) || [];
annotations.push(new Component(DecoratorUtils.getMetadata(metadata)));
_reflect.defineMetadata('annotations', annotations, cls);
return cls;
};
}
export class DecoratorUtils {
public static getMetadata(metadata: any = {}) {
metadata.encapsulation = ViewEncapsulation.None;
return metadata;
}
public static annotateComponent(cls: any, metadata: any = {}) {
let annotations = _reflect.getMetadata('annotations', cls) || [];
annotations.push(new Component(DecoratorUtils.getMetadata(metadata)));
_reflect.defineMetadata('annotations', annotations, cls);
return cls;
}
}
@nkmdev
Copy link

nkmdev commented Feb 26, 2017

How to use?

@spottedmahn
Copy link

// Usage:
// @basecomponent({ etc... })

@mkp05
Copy link

mkp05 commented Nov 23, 2017

Have you tried using such custom annotation with AOT compilation enabled? Because I receive
Please add a @Pipe/@Directive/@Component annotation
error when compiling with AOT enabled and wonder how to bypass/satisfy AOT verifications.

@iampratapak
Copy link

Any update on this? I'm also facing issue with AOT build. Any help would be really appreciable.

@davidmpaz
Copy link

@davcs86
Copy link

davcs86 commented Jan 10, 2018

I receive
Please add a @Pipe/@Directive/@Component annotation
even following the tips in @davidmpaz link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment