第一步,新建组件:

ionic g component product-img-list

命令执行成功之后项目中生成的文件:

第二步:生成文件解析:

①product-img-list.ts

import { Component, Input } from '@angular/core';

@Component({
selector: 'product-img-list',
templateUrl: 'product-img-list.html'
})
export class ProductImgListComponent {
//传入的参数
@Input() infos;
@Input() showView;
constructor() {
console.log(this.infos);
}
}

②product-img-list.html

<ion-grid [ngClass]="{'product-img': showView=='img','product-list':showView=='list'}">
<ion-row>
<ion-col col-6 col-sm-4 col-md-3 col-lg-3 col-xl-3 *ngFor="let p of infos">
<div class="p-des">
<img src={{p.images[0].url}} />
<p class="name">{{p.productName}}</p>
<p class="price">
<ion-icon name="aperture"></ion-icon>{{p.integral}}+{{p.vipPrice|PRICE}}</p>
</div>
</ion-col>
</ion-row>
</ion-grid>

③components.module.ts

import { NgModule } from '@angular/core';
import { EditUpdateNumberComponent } from './edit-update-number/edit-update-number';
import { ProductImgListComponent } from './product-img-list/product-img-list';
import { OrderAddressComponent } from './order-address/order-address'; @NgModule({
declarations: [
EditUpdateNumberComponent,
ProductImgListComponent,
OrderAddressComponent,
OrderAddressComponent],
imports: [],
exports: [
EditUpdateNumberComponent,
ProductImgListComponent,
OrderAddressComponent,
OrderAddressComponent]
})

第三步,在需要使用组件的页面添加一下代码:

<ion-content>
<!-- 传入组件需要的数据infos / showView 与组件中标明传入的属性一致 -->
<product-img-list [infos]="infos" [showView]="viewStyle"></product-img-list>
<ion-infinite-scroll [enabled]="isShouldEnable" (ionInfinite)="doInfinite($event)">
<ion-infinite-scroll-content></ion-infinite-scroll-content>
</ion-infinite-scroll>
</ion-content>

第四步,在需要使用该组件的页面的module.ts 文件的declarations中导入ProductImgListComponent ;

至此,运行正常,组件使用完成;

but

如果一个页面需要使用多个组件的时候,就需要在declarations中导入多个组件(EditUpdateNumberComponent、ProductImgListComponent……),无形中增加了代码量与后期维护成本。

So,不在declarations中单个导入组件,我们选择在需要使用该组件的页面的module.ts 文件的imports中导入ComponentsModule。运行报错!

这个报错的元素跟我们在组件中使用的元素有关,因为我在组件的html文件中国使用了ion-col标签元素。因此,我们需要导入相应的模块IonicModule,修改components.module.ts文件如下:

import { NgModule } from '@angular/core';
import { IonicModule } from 'ionic-angular';
import { EditUpdateNumberComponent } from './edit-update-number/edit-update-number';
import { ProductImgListComponent } from './product-img-list/product-img-list';
import { OrderAddressComponent } from './order-address/order-address'; @NgModule({
declarations: [
EditUpdateNumberComponent,
ProductImgListComponent,
OrderAddressComponent],
imports: [IonicModule,AppPipeModule],
    exports: [
EditUpdateNumberComponent,
ProductImgListComponent,
OrderAddressComponent]
})
export class ComponentsModule { }

根据组件需要导入相应的依赖即可。例如我在组件中使用了管道PRICE,需要导入再导入AppPipeModule

最后,在需要使用组件的页面的module.ts文件的imports中导入ComponentsModule即可。

最新文章

  1. 编译安装 Centos 7 x64 + tengine.2.0.3 (实测+笔记)
  2. [Q&amp;A] 在证书存储区中找不到清单签名证书
  3. H5新特性websocket
  4. verilog阻塞与非阻塞的初步理解(二)
  5. urlscan使用详解
  6. C语言基础课程 第一课 Linux环境配置小实战httpserver
  7. BarTender打印出来的条码与设计的不同如何处理
  8. jump堡垒机配置使用
  9. MongoDB优化与一些需要注意的细节
  10. 小白的Python之路 day5 shelve模块讲解
  11. 浅谈linux虚拟内存结构
  12. NodeJS技巧
  13. 一致性Hash漫画图解
  14. 利用反射将IDataReader读取到实体类中效率低下的解决办法
  15. java中53个关键字的意义及使用方法
  16. Web项目发布后字体文件找不到
  17. Kotlin 对象表达式和对象声明
  18. python中OS模块方法
  19. freemarker数据类型
  20. Xamarin Essentials教程使用加速度传感器Accelerometer

热门文章

  1. Windows7下设置定时启动(关闭)虚拟机
  2. Switch 语句
  3. 版本控制——TortoiseSVN (3)多版本发布
  4. Flexbox学习总结
  5. io调度策略noop的理解
  6. 两层嵌套list,选取list中最长的list的长度
  7. linux_Nginx日志
  8. JavaScript Hoisting
  9. mybatis自动生成java代码
  10. 基于百度地图SDK和Elasticsearch GEO查询的地理围栏分析系统(3)-前端实现