首页 文章

Angular组件和模块之间有什么区别

提问于
浏览
82

我一直在看视频和阅读文章,但这篇文章让我很困惑,在文章的开头说

Angular中的应用程序遵循模块化结构 . Angular应用程序将包含许多模块,每个模块专用于单一用途 . 通常,模块是一个有凝聚力的代码组,它与其他模块集成以运行Angular应用程序 . 模块从其代码中导出一些类,函数和值 . Component是Angular的基本块,多个组件将构成您的应用程序 . 模块可以是另一个模块的库 . 例如,作为主要Angular库模块的angular2 / core库将由另一个组件导入 .

他们是可以交换的条款吗?组件是模块吗?但不是反之?

6 回答

  • 122

    enter image description here

    最简单说明:

    Module 就像一个包含一个或多个名为Component,Service,Pipe的小容器的大容器

    A Component 包含:

    • HTML模板或HTML代码

    • 代码(TypeScript)

    • 服务:它是一个可重用的代码,由组件共享,因此不需要重写代码

    • 管道:它将数据作为输入并将其转换为所需的输出

    .

  • 10

    组件是 the template(view) + a class (Typescript code) containing some logic for the view + metadata(to tell angular about from where to get data it needs to display the template) .

    模块 basically group the related components, services together ,以便您可以拥有可以独立运行的功能块 . 例如,应用程序可以包含功能模块,用于为应用程序的特定功能(例如仪表板)分组组件,您可以在其他应用程序中轻松获取和使用 .

  • 30

    组件控件视图(html) . 他们还与其他组件和服务进行通信,为您的应用程序带来功能 .

    模块由一个或多个组件组成 . 他们不控制任何HTML . 您的模块声明属于其他模块的组件可以使用哪些组件,哪些类将由依赖项注入器注入,哪个组件被引导 . 模块允许您管理组件,为您的应用程序带来模块化 .

  • 17

    Angular Component

    A component是Angular应用程序的基本构建块之一 . 一个应用程序可以有多个组件 . 在普通应用程序中,组件包含HTML视图页面类文件,控制HTML页面行为的类文件和用于设置HTML视图样式的CSS / scss文件 . 可以使用 @Component decorator创建组件,该装饰器是 @angular/core 模块的一部分 .

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

    并创建一个组件

    @Component({selector: 'greet', template: 'Hello {{name}}!'})
    class Greet {
      name: string = 'World';
    }
    

    要创建一个组件或角度应用程序,这里是tutorial

    Angular Module

    angular module是一组角度基本构建块,如componentdirectivesservices等 . 一个应用程序可以有多个模块 .

    可以使用 @NgModule 装饰器创建模块 .

    @NgModule({
      imports:      [ BrowserModule ],
      declarations: [ AppComponent ],
      bootstrap:    [ AppComponent ]
    })
    export class AppModule { }
    
  • 2

    好吧,发布答案为时已晚,但我觉得要了解谁是 Angular 的初学者将是 easy . 我在演讲中给出的一个例子 .

    将您的角度应用视为 Building . 建筑物中可以包含N个 Apartments . Apartment 被视为 Module . 公寓的 N 数量 rooms ,对应于名为 Components 的Angular应用程序的构建块 .

    现在每个 Apartment (Module) 都会有 Rooms (Components)Lifts (Services) 来进行更大规模的进出公寓, Wires (Pipes) 可以移动信息并使其在公寓中变得有用 .

    您还将拥有像 swimming pool, tennis court 这样的所有建筑物居民共享的地方 . 因此,这些可以被视为SharedModule中的组件 .

    基本上,差异如下,

    enter image description here

    按照我的幻灯片了解building blocks of an Angular application

  • 9

    Angular 2中的模块是由组件,指令,服务等组成的模块 . 一个或多个模块组合在一起构成一个Application . 模块将应用程序分解为逻辑代码片段 . 每个模块执行单个任务 .

    Angular 2中的组件是您为要显示的页面编写逻辑的类 . 组件控制视图(html) . 组件与其他组件和服务通信 .

相关问题