前端开发框架之Angular自定义组件学习分享

创建组件

在components文件夹下创建一个数据库下载的公用组件。

打开命令行(使用vscode编辑器的小伙可以直接使用Ctrl+` 快捷键打开终端,然后一路跳转到components文件夹:

cd src\app\components

在此目录下执行指令:

ng g c es-download

上面指令的意思是创建一个名为es-download的组件,

使用上面的指令创建的组件是会前端培训被自动引用到components这个模块中的。
components.module.ts

import { EsDownloadComponent } from './es-download/es-download.component';  //引入组件@NgModule({  declarations: [..., EsDownloadComponent],//声明组件})

上面是在使用ng g c es-download指令时自动完成的

但若是想在其它的模块中使用这个es-download组件,还得将其导出。

导出的方式是将这个组件添加至components.module.ts文件的exports中:

@NgModule({  declarations: [..., EsDownloadComponent],  imports: [...],  exports: [..., EsDownloadComponent],
})export class ComponentsModule { }

组件的基础概念

查看es-download.component.ts

import { Component, OnInit } from '@angular/core';@Component({  selector: 'app-es-download',  templateUrl: './es-download.component.html',  styleUrls: ['./es-download.component.css']
})export class EsDownloadComponent implements OnInit {  constructor() { }  ngOnInit(): void {
  }
}

可以看到此处从@angular/core中引入Component装饰器;并且建立了一个类,用@Component修饰它;在@Component中,设置了selector自定义标签和template模板。组件的几个关键知识点如下:

组件与模块

模块是在组件之上的一层抽象,组件以及指令、管道、服务、路由等都能通过模块去组织。
Angular提供了@NgModule装饰器来创建模块,一个应用可以有多个模块,有且只有一个根模块(Root Module),其他模块叫做特性模块(Feature Module)
根模块是启动应用的入口模块,根模块必须通过bootstrap元数据来指定应用的根组件,然后通过bootstrapModule()方法来启动应用。
建立一个根模块,命名为AppModule,并将它保存为app,module.ts。
app.module.ts中通过@NgModule的bootstrap元数据指定AppComponent组件

import { NgModule } from '@angular/core';import { AppComponent } from './app.component';@NgModule({  declarations: [...],  imports: [...],  providers: [...],  bootstrap: [AppComponent]
})export class AppModule { }

AppComponent组件即为根组件。
再创建一个main.ts,利用platformBrowserDynamic().bootstrapModule()方法来启动根模块,并将AppComponent组件的内容展示到页面上。

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';import { AppModule } from './app/app.module';platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

引用es-download组件

由于我们最开始是将es-download组件引入到components这个模块中,并从这个模块中导出的,所以想要在其它模块中使用 es-download组件就得先引入components模块。
将根模块AppModule,作为父组件来引用一下es-download组件
首先在模块里引用:

import { NgModule } from '@angular/core';import { ComponentsModule } from './components/components.module';@NgModule({  declarations: [...],  imports: [...,    ComponentsModule,
    ],
})export class AppModule { }

引入了components模块就相当于是引入那个那个模块中的所有组件和方法。

使用es-download组件

根据selector: 'app-es-download',所以要使用es-download这个组件,需要在HTML中添加自定义标签
<app-es-download></app-es-download>,然后Angular便会在此标签中插入EsDownloadComponent组件中指定的模板。

import { Component, OnInit } from '@angular/core';@Component({
  selector: 'app-es-download',  templateUrl: './es-download.component.html',  styleUrls: ['./es-download.component.css']
})

组件交互

事件交互

由于es-download.component.html中的按钮有点击事件

<button
  style="..."
  nz-button
  nzType="primary"
  (click)="esDownload()">
  数据库下载</button>

所以es-download.component.ts中需要实例化一个用来订阅和触发自定义事件的EventEmitter类

import { Component, OnInit,Output,EventEmitter} from '@angular/core';//引入Output,EventEmitter@Component({  selector: 'app-es-download',  templateUrl: './es-download.component.html',  styleUrls: ['./es-download.component.css']
})export class EsDownloadComponent implements OnInit {  @Output() click = new EventEmitter(); //通过输出属性@Output将数据流向父组件......//点击事件函数esDownload() {    .......  }}

数据交互

父组件将数据通过属性绑定的方式流向子组件,子组件通过输入属性@Input获取来自父组件的数据。
父组件的html文件:

<app-es-download [name]="name" ></app-es-download>

子组件的ts文件:

import { Component, OnInit,Output,EventEmitter,Input} from '@angular/core';@Component({  selector: 'app-es-download',  templateUrl: './es-download.component.html',  styleUrls: ['./es-download.component.css']
})export class EsDownloadComponent implements OnInit {
  @Output() click = new EventEmitter();  @Input() name:'';

其中name数据是通过装饰器@Input来获取来自父组件的name对象,数据由父组件流出,在子组件中通过输入属性@Input完成数据的接收。

(0)

相关推荐

  • Angular Package Format (APF) v12.0 介绍

    官网 本文档描述了 npm 上当前可用的 Angular 框架包的结构和格式. 这种格式适用于分发 Angular 组件的包(如 Angular Material)以及在@angular 命名空间下发 ...

  • Angular library 学习笔记

    Use cases for Angular libraries Angular 库有 2 个常见用例: 构建可重用的组件库以在应用程序之间共享. 构建共享服务层功能 - 例如. 用于处理外部数据源(例 ...

  • Angular Lazy load学习笔记

    Lazy loading, also known as code splitting, lets you divide your JavaScript code into multiple chunk ...

  • 一些关于 SAP Spartacus 组件和模块延迟加载的问题和解答

    仅执行组件延迟(Component lazy load)加载是不是不起作用?进行这项工作是否需要任何设置? 考虑到我们在整个网站上使用的共享组件,我们想采用这种方法(即组件延迟加载).这样,我们可以只 ...

  • Angular 应用里的摇树优化 - tree shaking

    Tree Shakeable Providers and Services in Angular Angular 最近推出了一项新功能,Tree Shakeable Providers. Tree S ...

  • Angular 从入坑到挖坑 - 模块简介

    一.Overview Angular 入坑记录的笔记第七篇,介绍 Angular 中的模块的相关概念,了解相关的使用场景,以及知晓如何通过特性模块来组织我们的 Angular 应用 对应官方文档地址: ...

  • require后面不加default会报错

    在项目中用 require('./Download.vue') 引入一个组件的时缺少.default 会报错: Failed to mount component: template or rende ...

  • Angular 依赖注入学习笔记之工厂函数的用法

    网址:https://angular.institute/di We can transfer any data through our apps, transform it and replace ...

  • 前端开发框架之Bootstrap4的学习分享

    一.文字排版 在Bootstrap4中文字默认大小为16px,line-height:1.5,font-family为:"Helvetica Neue",所有的p元素margin- ...

  • 前端开发技术之css样式学习笔记分享

    概述 前端培训开发中将前端结构化,html 是文档结构.css 是设置样式(美化页面).js是逻辑结构 重点是 "选择器" 和 "盒子模型" 发展史 CSS1. ...

  • 前端开发框架jQuery的优势与基础知识分享

    jQuery,顾名思义是JavaScript和查询(Query),jQuery是免费.开源的.它可以简化查询DOM对象.处理事件.制作动画.处理Ajax交互过程且兼容多浏览器的javascript库, ...

  • 【前端学习分享】HTML+CSS京东商城静态页面

    注意事项 一.CSS的定位问题position static 没有定位,遵循正常的文档流对象. 以下使用较多 fixed 相对于浏览器窗口是固定的,窗口的他不会移动(比如说百度弹出来的登录窗口,不会移 ...

  • 前端开发框架Vue中Vuex的使用原理分享

    在前端培训开发工作中,Vue.js的使用是主流技术,尤其是项目开发过程中只要使用到涉及Vue的状态管理就必然会用到Vuex.本篇博文就来分享一下关于Vuex的相关知识点,方便后期查阅使用. 1.首先来 ...

  • 前端开发框架之BootStrap入门学习

    Bootstrap是一组用于网站和网络应用程序开发的开源前端(所谓"前端",指的是展现给最终用户的界面.与之对应的"后端"是在服务器上面运行的代码)框架,包括H ...

  • 什么是古树普洱茶?干货知识点,值得学习分享

    普洱茶冲泡份量:冲泡普洱时茶叶量大约占壶身 20%.将茶砖 .茶饼 , 拨开后暴露空气 2 星期 , 再冲泡味道更好. 一饼优质普洱茶,一定是以优 秀的制茶工艺打底的,尤其是拼配技术.借拼配之手,不同 ...

  • 2020-1.1关于周易-易经学习分享

    今天是2020年1月1日,我先向所有的朋友们送上一份美好的祝福,愿大家及家人们身体康健,万事如意,心想事成! 今天要写的是周易,也就是易经,很多朋友一听说易经就认为易经是用来算命的,易经是迷信,其实了 ...