本篇文章為大家展示了Angular中怎么封裝一個并發(fā)布組件,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站服務(wù)項目包括宿州網(wǎng)站建設(shè)、宿州網(wǎng)站制作、宿州網(wǎng)頁制作以及宿州網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,宿州網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到宿州省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
一、封裝組件
目錄結(jié)構(gòu):(部分目錄不是框架中自動生成,二是后期添加,按照步驟進行即可。)
具體代碼:
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>
對應(yīng)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)整組件的目錄結(jié)構(gòu),如最上圖所示,這是規(guī)范的目錄結(jié)構(gòu),調(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、對應(yīng)的ts中:
注意:
上述內(nèi)容就是Angular中怎么封裝一個并發(fā)布組件,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:Angular中怎么封裝一個并發(fā)布組件
瀏覽路徑:http://muchs.cn/article2/jpigic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、品牌網(wǎng)站設(shè)計、響應(yīng)式網(wǎng)站、網(wǎng)站策劃、網(wǎng)站建設(shè)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)