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

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


当前回答

简单的回答

组件:一个主要的构建块,用于添加一些DOM元素/Html。

指令:用于在DOM元素/HTML中添加一些表达式、条件和循环。

其他回答

在Angular 2及以上版本中,“所有东西都是组件。”组件是 我们在页面上构建和指定元素和逻辑的主要方式, 通过添加功能的自定义元素和属性 我们现有的组件。

http://learnangular2.com/components/

但是在Angular2+中指令是做什么的呢?

属性指令将行为附加到元素上。 Angular中有三种指令: 组件——带有模板的指令。 结构性directives-change 通过添加和删除DOM元素来调整DOM布局。 属性指令——改变元素的外观或行为, 组件或另一个指令。

https://angular.io/docs/ts/latest/guide/attribute-directives.html

在Angular2和以上版本中,指令是为元素和组件添加功能的属性。

看看下面Angular.io的示例:

import { Directive, ElementRef, Input } from '@angular/core';

@Directive({ selector: '[myHighlight]' })
export class HighlightDirective {
    constructor(el: ElementRef) {
       el.nativeElement.style.backgroundColor = 'yellow';
    }
}

它会扩展你的组件和HTML元素,添加黄色背景,你可以像下面这样使用:

<p myHighlight>Highlight me!</p>

但是组件将创建具有所有功能的完整元素,如下所示:

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

@Component({
  selector: 'my-component',
  template: `
    <div>Hello my name is {{name}}. 
      <button (click)="sayMyName()">Say my name</button>
    </div>
   `
})
export class MyComponent {
  name: string;
  constructor() {
    this.name = 'Alireza'
  }
  sayMyName() {
    console.log('My name is', this.name)
  }
}

你可以这样使用它:

<my-component></my-component>

当我们在HTML中使用标记时,该组件将被创建,构造函数将被调用并呈现。

如果你参考官方的angular文档

https://angular.io/guide/attribute-directives

Angular中有三种指令:

组件——带有模板的指令。 结构指令——通过添加和删除DOM元素来改变DOM布局。如* ngIf 属性指令——改变元素、组件或其他指令的外观或行为。如[ngClass]。

随着应用程序的增长,我们发现很难维护所有这些代码。出于可重用的目的,我们在智能组件和哑组件中分离了逻辑,并使用指令(结构或属性)在DOM中进行更改。

简单的回答

组件:一个主要的构建块,用于添加一些DOM元素/Html。

指令:用于在DOM元素/HTML中添加一些表达式、条件和循环。

@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>

查看更多关于指令的信息