本篇文章為大家展示了Angular中怎么封裝一個并發(fā)布組件,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
一、封裝組件
目錄結構:(部分目錄不是框架中自動生成,二是后期添加,按照步驟進行即可。)
具體代碼:
html:(search.component.html)
<input type="text" class="form-control" #info placeholder="{{information}}" > <button type="button" class="btn btn-default" (click)="query(info.value);">查詢</button> css:(search.component.css) .form-control{ float: left; width: 70%; } .btn btn-default{ background-color: #41ABE9; }
ts:(search.component.ts)
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core'; @Component({ selector: 'app-search', templateUrl: './search.component.html', styleUrls: ['./search.component.css'] }) export class SearchComponent implements OnInit { @Input() information: string; @Input() url: string; dataUrl: string; @Output() editData = new EventEmitter<any>(); constructor() { } ngOnInit() { } query(info: string) { this.dataUrl = this.url + '/' + info; this.editData.emit(this.dataUrl); } }
解釋:@Input,接收信息。如information可以接收Html中的{{information}}的值
@Output是輸出。即引用組件化的人可以拿到editData的返回值。
module:(search.module.ts)
import {SearchComponent} from './search.component' ; import {CommonModule} from '@angular/common'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { HttpModule } from '@angular/http'; @NgModule({ declarations: [ SearchComponent ], imports: [ CommonModule, FormsModule, HttpModule, ], providers: [], exports: [SearchComponent], }) export class SearchModule { }
至此組件完成,可以通過在app.component.html中引入如下看看效果:
<h2> {{information}} {{dataUrl}} </h2> <div > <app-search [information]="information" [url]="url" (editData)="query($event)"></app-search> </div>
對應app.component.ts中需要定義:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { information = '輸入班級名稱'; url= 'Class/find'; dataUrl: string; query(info: any) { this.dataUrl = info; } }
點擊查詢后效果如:
二、發(fā)布,供大家引用
1、注冊npm賬號:
地址:https://www.npmjs.com
2、手動或者命令創(chuàng)建package.json文件
內(nèi)容包括:
3、手動或命令創(chuàng)建index.js文件
在添加內(nèi)容前,我們調(diào)整組件的目錄結構,如最上圖所示,這是規(guī)范的目錄結構,調(diào)整好后,添加index.js內(nèi)容:
export * from './lib/search.module';
4、手動或命令創(chuàng)建index.d.ts文件
export {SearchModule} from './search.module';
5、Ctrl+Shift+右擊(在search組件目錄下)
運行:npm login
輸入賬號、密碼、郵箱
登錄成功后:運行npm publish
至此發(fā)布完成。
三、引用者調(diào)用:
1、Ctrl+Shift+右擊(項目根目錄)
cnpm install ng-itoo-search
2、引入到項目中
自己的Module中
3、自己的Html中:
<app-search [information]="information" [url]="url " (editData)="query($event)"></app-search>
4、對應的ts中:
注意:
上述內(nèi)容就是Angular中怎么封裝一個并發(fā)布組件,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前名稱:Angular中怎么封裝一個并發(fā)布組件-創(chuàng)新互聯(lián)
本文地址:http://www.chinadenli.net/article20/didijo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、微信小程序、品牌網(wǎng)站制作、用戶體驗、移動網(wǎng)站建設、手機網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容