I have a component
@Component({
// todo the app-old-selector selector must be removed in the next version
selector: 'app-new-selector,app-old-selector',
templateUrl: './component.html'
})
export class Component {
}
What is the best way to inform a developer that app-old-selector
is deprecated?
Angular strives to balance innovation and stability. Sometimes, APIs and features become obsolete and need to be removed or replaced so that Angular can stay current with new best practices, changing dependencies, or changes in the (web) platform itself.
ComponentFactorylinkBase class for a factory that can create a component dynamically. Instantiate a factory for a given type of component with resolveComponentFactory() . Use the resulting ComponentFactory. create() method to create a component of that type. Deprecated: Angular no longer requires Component factories.
A selector is one of the properties of the object that we use along with the component configuration. A selector is used to identify each component uniquely into the component tree, and it also defines how the current component is represented in the HTML DOM.
::ng-deep is what's called a shadow-piercing descendant combinator. It works because it is able to pierce the shadow DOM. Using ::ng-deep to override a child components styles will achieve the desired result but with unintended side effects.
I have written a reusable decorator which marks components' selectors as deprecated.
import {Component} from '@angular/core';
type Constructor<T = {}> = new (...args: any[]) => T;
export function Deprecated(oldSelector: string) { // This is a decorator factory
return <T extends Constructor>(Base: T) => {
return class Deprecated extends Base {
selectors = [];
constructor(...args: any[]) {
super(...args);
const selector = new Component((Deprecated as any).__annotations__[0]).selector;
this.selectors = selector.split(', ');
this.selectors = this.selectors.filter(selector => selector !== oldSelector);
console.warn('The selector "' + oldSelector + '" is going to be deprecated. Please use one of these selectors [ ' + this.selectors.toString() + ' ]');
}
};
};
}
Now we just have to decorate our component class with this decorator function with parameter like below
@Component({
selector: 'my-old-app, my-app-new',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
@Deprecated("my-old-app")
export class AppComponent {
name = 'Angular';
}
Please find the code here at stackblitz.
Also, please read my blog on the same topic which has the explanation on the logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With