Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 10 Upgrade: Why use @Directive() instead of @Component() for abstract (component) classes?

I recently upgraded my Angular app from v9 to v10.

I noticed that undecorated classes are not supported anymore. See here

So during ng upgrade my abstract components without decorates have been changed to have a @Directive() decoratror.

For example

export abstract class AbstractFormControl implements ControlValueAccessor { ... }

was changed into

@Directive()
export abstract class AbstractFormControl implements ControlValueAccessor { ... }

Why does Angular use @Directive. Wouldn't @Component be a better way because the class is rather a Component than a Directive? What was the intention?

like image 535
Rose Nettoyeur Avatar asked Sep 01 '20 11:09

Rose Nettoyeur


People also ask

What is the difference between directive and component in Angular?

The Component is used to break up the application into smaller components. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model. The Directive is used to design reusable components, which are more behavior-oriented.

Can directive be used as component?

A component is a single unit that encapsulates both view and logic whereas directives are used to enhance the behavior of components or dom elements and it doesn't have any templates. Component extends directive so every component is a directive.

What is the difference between directive and decorator in Angular?

In Angular, a Directive is essentially a typescript class which has been annotated with a TypeScript Decorator. The decorator is the @ symbol. Decorators are not currently part of the JavaScript functionality (although they likely will be in the future) and are also still experimental in TypeScript.

Is component a directive in Angular?

Component directive is used to specify the HTML templates. It has structure design and the working pattern of how the component should be processed, instantiated and used at runtime. It is the most commonly-used directive in any Angular project.


1 Answers

Component requires you to specify template or templateUrl, which cannot be used with an abstract class.

As mentioned in the comments - Component is a special type of Directive. So it is okay to inherit from an abstract Directive.

like image 156
kvetis Avatar answered Oct 16 '22 07:10

kvetis