日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

前端開(kāi)發(fā)框架之Angular自定義組件學(xué)習(xí)分享

 IT小白在線 2021-10-29

創(chuàng)建組件

在components文件夾下創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)下載的公用組件。

打開(kāi)命令行(使用vscode編輯器的小伙可以直接使用Ctrl+` 快捷鍵打開(kāi)終端,然后一路跳轉(zhuǎn)到components文件夾:

cd src\app\components

在此目錄下執(zhí)行指令:

ng g c es-download

上面指令的意思是創(chuàng)建一個(gè)名為es-download的組件,

使用上面的指令創(chuàng)建的組件是會(huì)前端培訓(xùn)被自動(dòng)引用到components這個(gè)模塊中的。
components.module.ts

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

上面是在使用ng g c es-download指令時(shí)自動(dòng)完成的

但若是想在其它的模塊中使用這個(gè)es-download組件,還得將其導(dǎo)出。

導(dǎo)出的方式是將這個(gè)組件添加至components.module.ts文件的exports中:

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

組件的基礎(chǔ)概念

查看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裝飾器;并且建立了一個(gè)類,用@Component修飾它;在@Component中,設(shè)置了selector自定義標(biāo)簽和template模板。組件的幾個(gè)關(guān)鍵知識(shí)點(diǎn)如下:

組件與模塊

模塊是在組件之上的一層抽象,組件以及指令、管道、服務(wù)、路由等都能通過(guò)模塊去組織。
Angular提供了@NgModule裝飾器來(lái)創(chuàng)建模塊,一個(gè)應(yīng)用可以有多個(gè)模塊,有且只有一個(gè)根模塊(Root Module),其他模塊叫做特性模塊(Feature Module)
根模塊是啟動(dòng)應(yīng)用的入口模塊,根模塊必須通過(guò)bootstrap元數(shù)據(jù)來(lái)指定應(yīng)用的根組件,然后通過(guò)bootstrapModule()方法來(lái)啟動(dòng)應(yīng)用。
建立一個(gè)根模塊,命名為AppModule,并將它保存為app,module.ts。
app.module.ts中通過(guò)@NgModule的bootstrap元數(shù)據(jù)指定AppComponent組件

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

AppComponent組件即為根組件。
再創(chuàng)建一個(gè)main.ts,利用platformBrowserDynamic().bootstrapModule()方法來(lái)啟動(dòng)根模塊,并將AppComponent組件的內(nèi)容展示到頁(yè)面上。

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

引用es-download組件

由于我們最開(kāi)始是將es-download組件引入到components這個(gè)模塊中,并從這個(gè)模塊中導(dǎo)出的,所以想要在其它模塊中使用 es-download組件就得先引入components模塊。
將根模塊AppModule,作為父組件來(lái)引用一下es-download組件
首先在模塊里引用:

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

引入了components模塊就相當(dāng)于是引入那個(gè)那個(gè)模塊中的所有組件和方法。

使用es-download組件

根據(jù)selector: 'app-es-download',所以要使用es-download這個(gè)組件,需要在HTML中添加自定義標(biāo)簽
<app-es-download></app-es-download>,然后Angular便會(huì)在此標(biāo)簽中插入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中的按鈕有點(diǎn)擊事件

<button
  style="..."
  nz-button
  nzType="primary"
  (click)="esDownload()">
  數(shù)據(jù)庫(kù)下載</button>

所以es-download.component.ts中需要實(shí)例化一個(gè)用來(lái)訂閱和觸發(fā)自定義事件的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(); //通過(guò)輸出屬性@Output將數(shù)據(jù)流向父組件......//點(diǎn)擊事件函數(shù)esDownload() {    .......  }}

數(shù)據(jù)交互

父組件將數(shù)據(jù)通過(guò)屬性綁定的方式流向子組件,子組件通過(guò)輸入屬性@Input獲取來(lái)自父組件的數(shù)據(jù)。
父組件的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數(shù)據(jù)是通過(guò)裝飾器@Input來(lái)獲取來(lái)自父組件的name對(duì)象,數(shù)據(jù)由父組件流出,在子組件中通過(guò)輸入屬性@Input完成數(shù)據(jù)的接收。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多