Angular中的@Component和@Directive有什么区别? 它们似乎都做同样的任务,具有相同的属性。

用例是什么,什么时候选择一个而不是另一个?


当前回答

组件

要注册一个组件,我们使用@Component元数据注释。 Component是一个指令,它使用shadow DOM创建封装的可视行为,称为组件。组件通常用于创建UI小部件。 组件用于将应用程序分解为更小的组件。 每个DOM元素只能有一个组件。 @View decorator或templateurl template在组件中是必选项。

指令

要注册指令,我们使用@Directive元数据注释。 指令用于向现有DOM元素添加行为。 指令用于设计可重复使用的组件。 每个DOM元素可以使用许多指令。 指令不使用视图。

来源:

https://www.devdiscuss.com/difference-between-component-and-directive-in-angular-2/

其他回答

@Component需要一个视图,而@Directive不需要。

指令

我把@Directive比作带有选项restrict: ' a '的Angular 1.0指令(指令不局限于属性的使用)。指令向现有DOM元素或现有组件实例添加行为。指令的一个示例用例是记录对元素的单击。

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

@Directive({
    selector: "[logOnClick]",
    hostListeners: {
        'click': 'onClick()',
    },
})
class LogOnClick {
    constructor() {}
    onClick() { console.log('Element clicked!'); }
}

可以这样使用:

<button logOnClick>I log when clicked!</button>

组件

组件,而不是添加/修改行为,实际上创建自己的视图(DOM元素的层次结构)与附加的行为。一个示例用例可能是一个联系人卡组件:

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

@Component({
  selector: 'contact-card',
  template: `
    <div>
      <h1>{{name}}</h1>
      <p>{{city}}</p>
    </div>
  `
})
class ContactCard {
  @Input() name: string
  @Input() city: string
  constructor() {}
}

可以这样使用:

<contact-card [name]="'foo'" [city]="'bar'"></contact-card>

ContactCard是一个可重用的UI组件,我们可以在应用程序的任何地方使用它,甚至可以在其他组件中使用。这些基本构成了应用程序的UI构建块。

总之

当你想用自定义行为创建一组可重用的UI DOM元素时,编写一个组件。当您希望编写可重用行为以补充现有DOM元素时,请编写一个指令。

来源:

@Directive文档 @ component文档 有用的博客文章

组件

组件是Angular应用中最基本的UI构建块。Angular应用中包含一个Angular组件树。我们在Angular中的应用是构建在一个组件树上的。每个组件都应该有自己的模板、样式、生命周期、选择器等等。因此,每个组件都有自己的结构,你可以将它们视为一个独立的小型web应用程序,有自己的模板和逻辑,并有可能与其他组件通信和一起使用。

Component的.ts文件示例:

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

@Component({
    // component attributes
    selector: 'app-training',
    templateUrl: './app-training.component.html',
    styleUrls: ['./app-training.component.less']
})

export class AppTrainingComponent {
    title = 'my-app-training';
}

以及它的。/app.component.html模板视图:

Hello {{title}}

然后你可以在其他组件中呈现AppTrainingComponent模板及其逻辑(在将其添加到模块后)

<div>
   <app-training></app-training>
</div>

结果就是

<div>
   my-app-training
</div>

as AppTrainingComponent在这里被渲染

有关组件的详细信息

指令

指令更改现有DOM元素的外观或行为。例如[ngStyle]是一个指令。指令可以扩展组件(可以在组件内部使用),但它们不能构建整个应用程序。假设它们只支持组件。它们没有自己的模板(当然,您可以使用它们来操作模板)。

示例指令:

@Directive({
  selector: '[appHighlight]'
})
export class HighlightDirective {

  constructor(private el: ElementRef) { }

  @Input('appHighlight') highlightColor: string;

  @HostListener('mouseenter') onMouseEnter() {
    this.highlight(this.highlightColor || 'red');
  }

  private highlight(color: string) {
    this.el.nativeElement.style.backgroundColor = color;
  }
}

及其用法:

<p [appHighlight]="color" [otherPar]="someValue">Highlight me!</p>

查看更多关于指令的信息

这是Angular 13的最新更新

@Component只是@Directive的一个子类。在深入研究这个问题之前,我们必须了解什么是@Directive…

@Directive是一个装饰器,用于指示DOM添加新元素或删除或修改现有元素。因此,无论何时Angular遇到任何装饰器,它都会在运行时处理它们,并根据它们修改DOM。

我们可以使用@Directive创建我们的指令,如下所示

@Directive({
  selector: '[demoButtonColor]'
})
export class DemoButtonColorDirective {
  constructor(private elementRef: ElementRef) { };
  ngOnInit() {
    this.elementRef.nativeElement.style.backgroundColor = 'red';
  }
}

在HTML中使用

<button demoButtonColor>RED BUTTON</button>

现在让我们看看什么是@Component decorator

@Component是@Directive的一个子类,有一个额外的功能。使用@Component,我们可以创建HTML模板,它可以在运行时注入到DOM中。

@Component({
  selector: 'demo-color',
  template: '<h1>Hello There!</h1>'
})
class DemoColorComponent {}

我们可以在任何其他组件中重用它,如下所示

<div>
  <demo-color></demo-color>
</div>

要将其包装起来,使用@Directive创建一个可用于修改DOM元素或结构的自定义指令。如果你想创建具有自定义行为的可重用UI组件,请使用@Component。

组件

要注册一个组件,我们使用@Component元数据注释。 Component是一个指令,它使用shadow DOM创建封装的可视行为,称为组件。组件通常用于创建UI小部件。 组件用于将应用程序分解为更小的组件。 每个DOM元素只能有一个组件。 @View decorator或templateurl template在组件中是必选项。

指令

要注册指令,我们使用@Directive元数据注释。 指令用于向现有DOM元素添加行为。 指令用于设计可重复使用的组件。 每个DOM元素可以使用许多指令。 指令不使用视图。

来源:

https://www.devdiscuss.com/difference-between-component-and-directive-in-angular-2/

在编程环境中,指令为编译器提供指导,以改变它处理输入的方式,即改变某些行为。

指令允许你将行为附加到DOM中的元素上。

指令分为3类:

属性 结构 组件

是的,在Angular 2中,组件是一种指令。 据医生说,

“Angular组件是指令的一个子集。与指令不同,组件总是有一个模板,并且模板中的每个元素只能实例化一个组件。”

Angular 2组件是Web组件概念的实现。Web组件由几个独立的技术组成。您可以将Web组件看作是使用开放Web技术创建的可重用用户界面小部件。

在摘要指令中,我们将行为附加到DOM中的元素的机制,包括结构化的, 属性和组件类型。 组件是特定类型的指令,允许我们 利用web组件功能即可重用性- 封装的、可重用的元素可在整个应用程序中使用。