Angular2: Changing default component properties.

Attention! Please keep in mind that this post is not manual, it’s research that helps you to understand Angular better and don’t try to apply code examples in your enterprise applications.

If you look into all the angular exports you could find specific super private(double low dash) property __core_private__ :

import { __core_private__ } from '@angular/core';

it provides us access to a method makeDecorator that is kind of internal factory to make all the decorators, so to create new decorator you just need to do:

__core_private__.makeDecorator('myNewDecorator', {});

so if you wanna create your own Component decorator it’s just:

const MyComponent = __core_private__.makeDecorator('MyComponent', {});

there is also 3rd parameter that allows us to make inheritance from another decorator, so let’s use existing Component decorator:

const MyComponent = __core_private__.makeDecorator('MyComponent', {}, Component);

we need to set required properties(it will not work without them):

const MyComponent = __core_private__.makeDecorator('myComponent',{
selector: undefined,
template: '',
styleUrls: undefined},Component))

and you could also extend some extra, for example switch off encapsulation:

const MyComponent = __core_private__.makeDecorator('myComponent',{
selector: undefined,
template: '',
styleUrls: undefined
encapsulation: ViewEncapsulation.None

All code for module(mycomponent.ts):

import {__core_private__, Component, ViewEncapsulation} from '@angular/core';

let MyComponent = __core_private__.makeDecorator('myComponent', {
 selector: undefined,
 template: '',
 styleUrls: undefined,
 encapsulation: ViewEncapsulation.None},

export {MyComponent as Component}

that can be use instead of core Component:

import {Component} from './mycomponent';