Angular 封装并发布组件的方法示例

所属分类: 网络编程 / JavaScript 阅读数: 1630
收藏 0 赞 0 分享

一、封装组件

作为入门,这是一个非常简单的demo,但核心的接收使用者的输入@Input(),以及返回数据给使用者@Output()都实现了,所以有一定的借鉴意义。

目录结构:(部分目录不是框架中自动生成,二是后期添加,按照步骤进行即可。)

具体代码:

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中引入如下看看效果:

<h1> 
 {{information}} 
 {{dataUrl}} 
</h1> 
<div style="width: 300px;padding-left: 5px"> 
<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; 
 } 
} 

点击查询后效果如:

二、发布,供大家引用

1、注册npm账号:

地址:https://www.npmjs.com

2、手动或者命令创建package.json文件

内容包括:

3、手动或命令创建index.js文件

在添加内容前,我们调整组件的目录结构,如最上图所示,这是规范的目录结构,调整好后,添加index.js内容:

export * from './lib/search.module'; 

4、手动或命令创建index.d.ts文件

export {SearchModule} from './search.module'; 

5、Ctrl+Shift+右击(在search组件目录下)

    运行:npm login

    输入账号、密码、邮箱

    登录成功后:运行npm publish

    至此发布完成。

三、引用者调用:

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中:

注意:

框中的url和ts中保持一致即可,并非必须写url,自己定义。

OK,现在完整的一个组件就开发、发布完成了。这样就可以让其他开发人员引用了。通过这样的封装,既可以实现代码的复用,又会减少项目打包的体积......是Angular的一大优点。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

jQuery 行级解析读取XML文件(附源码)

项目中应用jQuery解析读取XML语言配置文件来实现语言的调度。这是jQuery解析读取XML文件功能的测试源码,现拿出来分享。
收藏 0 赞 0 分享

JS 文件本身编码转换 图文教程

JS编码转换,这句话本身就是一句具有二重义的话。通常理解为JS文件里能转换编码的代码,但是,我所碰到的问题并不是这样的,是要解决JS文件本身的编码问题,它是UTF-8编码的还是ANSI编码的?
收藏 0 赞 0 分享

jQuery Ajax之$.get()方法和$.post()方法

load()方法通常用来从Web服务器上获取静态的数据文件,然而这并不能体现Ajax的全部价值。在项目中,如果需要传递一些参数给服务器中的页面,那么可以使用$.get()或者$.post()方法(或者是后面要讲解到的$.ajax方法)。
收藏 0 赞 0 分享

jQuery Ajax之load()方法

jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是laod()、$.get()和$.post()方法,第3层是$.getScript()和$.getJSON()方法。
收藏 0 赞 0 分享

JavaScript 核心参考教程 内置对象

JavaScript 是根据 "ECMAScript"标准制定的网页脚本语言。这个标准由 ECMA 组织发展和维护。ECMA-262 是正式的 JavaScript 标准。
收藏 0 赞 0 分享

JavaScript 核心参考教程 RegExp对象

JavaScript 核心参考教程RegExp对象,学习正则表达式的朋友可以参考下。
收藏 0 赞 0 分享

javascript hashtable实现代码

javascript中没有像c#,java那样的哈希表(hashtable), 然而,javascript中的Array也只有一些类似于'哈希表'的非常简单功能。
收藏 0 赞 0 分享

百度留言本js 大家可以参考下

百度留言本js 大家可以参考下。
收藏 0 赞 0 分享

javascript 判断某年某月有多少天的实现代码 推荐

以前写网页的时候,经常碰到选择日期的问题,其实就是判断某年某月有多少天。
收藏 0 赞 0 分享

让iframe子窗体取父窗体地址栏参数(querystring)

突然用到,记录一下,对地址栏字符串用正则处理最好,有时间研究一下。 主要是思路。
收藏 0 赞 0 分享
查看更多