美文网首页hybrid APP(ionic)Ionic3项目实战教程ionic开发
ionic3项目实战教程 - 第6讲 ionic3 compon

ionic3项目实战教程 - 第6讲 ionic3 compon

作者: IonicBlog | 来源:发表于2017-08-22 20:35 被阅读4143次

这一讲主要包含以下几个部分:

  • 1.创建ionic component
  • 2.使用ionic component

1.创建ionic3组建

1.1 组建的创建

执行 ionic g component ion-products ,等待30s左右,成功之后可以看到目录会发生变化:

6-1.png

1.2 组建的数据交互

通过@Input()来实现数据交互,ion-products.ts完整代码如下:

ion-products.ts

import { NavController } from 'ionic-angular';
import { Component, Input } from '@angular/core';
@Component({
  selector: 'ion-products',
  templateUrl: 'ion-products.html'
})
export class IonProductsComponent {
  @Input() products: Array<any>;
  constructor(public navCtrl: NavController) {
    console.log('Hello IonProducts Component');
  }
  goDetails(item) {
    console.debug('go details...')
  }
} 

1.3 组建的html实现

将home.html中商品列表部分的代码剪切到ion-products.html

ion-products.html

  <div class="product">
    <ion-row wrap>
      <ion-col tappable col-6 *ngFor="let p of products" (click)="goDetails(p)">
         ![]({{p.PictUrl}})
        <p>{{p.Title}}</p>
        <div class="list-price buy">
          <span class="price-new"><i>¥</i>{{p.ZkFinalPrice}}</span>
          <i class="del">¥<span>{{p.ReservePrice}}</span></i>
        </div>
      </ion-col>
    </ion-row>
  </div>

1.4 组建样式的实现

将home.scss里关于商品样式的代码剪切到ion-products.scss

ion-products.scss

  ion-products {
  .product {
    ion-row {
      background-color: #efefef;
      font-family: "Helvetica Neue", Helvetica, STHeiTi, sans-serif;
      padding-top: 3px;
      ion-col {
        position: relative;
        background-color: white;
        border: 2px solid #efefef;
        border-radius: 2px;
        padding: 0px;
        p {
          margin: 0px;
          padding: 0px;
          width: 100%;
          font-size: 12px;
          font-family: "黑体-简";
          font-weight: 300;
          color: #808080;
          height: 26px;
          line-height: 26px;
          background: rgba(255, 255, 255, 0.8);
          overflow: hidden;
          text-indent: 5px;
        }
      }
    }
    .list-price {
      width: 98%;
      height: 26px;
      line-height: 18px;
      position: relative;
      bottom: 0;
      margin: 0 2%;
    }
    .price-new {
      font-size: 18px;
      color: #f8285c;
    }
    .list-price i {
      font-style: normal;
      font-size: 12px;
    }
    .del {
      color: rgba(171, 171, 171, 1);
      text-decoration: line-through;
      margin-left: 2px;
    }
    .good-btn {
      display: block;
      position: absolute;
      height: 16px;
      line-height: 12px;
      color: #f8285c;
      font-size: 13px;
      font-family: "黑体-简";
      text-align: right;
      top: 5px;
      right: 2px;
    }
    .quan_img {
      position: absolute;
      width: 61px;
      height: 55px;
      z-index: 5;
      right: 0;
      top: 0;
      cursor: pointer;
      background: url(/assets/img/quan.png) no-repeat;
      background-size: contain;
      color: #fff;
    }
    .quan_img .num_money {
      font-family: Arial;
      font-size: 18px;
      height: 40px;
      left: 9px;
      position: absolute;
      text-align: center;
      top: 14px;
      width: 40px;
    }
  }
}

分别完成 ion-products.ts , ion-products.html , ion-products.scss,组建就创建完毕了。

接下来教大家如何使用组建。

2.ionic3�组建的使用

组建创建成功时候,可以看到默认有一个�components.module.ts,�说明此component建默认是支持懒加载的。

components.modules.ts

import { IonicModule } from 'ionic-angular';
import { NgModule } from '@angular/core';
import { IonProductsComponent } from './ion-products/ion-products';

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

2.1 导入component

在home.module.ts中导入components.module.ts

home.module.ts

import { ComponentsModule } from './../../components/components.module';
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { HomePage } from './home';
@NgModule({
    declarations: [
        HomePage
    ],
    imports: [
        IonicPageModule.forChild(HomePage),ComponentsModule
    ],
})
export class HomePageModule { }

导入之后才可以在home.html中使用。

2.2 使用component

将home.html的商品列表部分换成ion-products来呈现

 <ion-item-divider class="t-header" color="light"> 年会礼服2017年新款 </ion-item-divider>
<ion-products [products]="products"></ion-products>

好了,试试看效果。

6-2.png

至于为什么要封装,封装有什么好处,下一讲用实例告诉大家。

完!

相关文章

网友评论

  • 47f9c54ea954:楼主,API还是不能用吗???
  • 23a18862452a:引入component的页面返回按钮样式就和其他页面的不一样了,返回按钮文字变back。在app.module.ts修改无果,在componente.module.ts设置backButtonText:'',backButtonIcon: 'arrow-dropleft',只生效了返回按钮文本为空
  • 差不多先生_2517:请问怎么在home页面修改组件样式?
  • 2d6182223e59:大神,<ion-products [products]="products"></ion-products>
    中的[products]和"products"各代表什么意思?[products]是固定语法吗?"products"是变量吗?我用命令定义别的封装名称,应该改成哪个?
    IonicBlog:[products]是ion-products组建的一个属性,"products"是在给这个组建的products属性赋值。
  • ef15d9302f9b:请问一下自定义组件用this.modalCtrl.create'myComponent)可以懒加载吗?好像怎么写都报错
  • 林千景:报错:Can't bind to 'products' since it isn't a known property of 'ion-products'.
    8883c9e07f61:@IonicBlog 可以了,谢谢:stuck_out_tongue_closed_eyes:
    IonicBlog:@蝶蝶不貅 试试用npm install一下依赖包
    8883c9e07f61:我也是
  • 窝壳里的牛:请问 为什么我把代码剪切到ion-component,html中时候 会报错误
    message: ''ion-grid' is not a known element
    请问这个是什么原因?
    85732d8c0045:import { NgModule } from '@angular/core';
    import { IonicModule } from 'ionic-angular';
    import { IonProductListComponent } from './ion-product-list/ion-product-list';
    @NgModule({
    declarations: [IonProductListComponent],
    imports: [IonicModule],
    exports: [IonProductListComponent]
    })
    export class ComponentsModule {}
    看见imports里面的了吗?加进去
  • 据呵呵:大神,组件模板里面的 ![]({{p.PictUrl}}) 是加载图片吗,这个是什么意思,我没有明白
    1060bbb5813e:<img src>就会变成这样 我还是不行。
    1060bbb5813e:markdown自动改的
  • 据呵呵:为什么要加components.module 这段,你是想吧所有的组件放到components.module里面加载吗?他原生的不是生成一个ion-products.module的懒加载吗,这样写components有什么好处吗?
    IonicBlog:是的,对于组建,我习惯一并加载,你说的方法也是可行的,看项目体量,和单个组建的复杂程度来决定组建是一起加载,还是各自懒加载。
  • 1060bbb5813e:你好,按照你的步骤走白屏无法进入页面 浏览器输出为Can't bind to 'products' since it isn't a known property of 'ion-products'.
    据呵呵:@日久不生情 你可以在命令行生成的 ion-products.module 里面进行懒加载,这样是不会报错的
    1060bbb5813e:@TongeBlog 添加上了 浏览器报一样的错--:sweat:
    IonicBlog:不好意思,少了一段代码,已经补充,请参见第2小节的components.modules.ts

本文标题:ionic3项目实战教程 - 第6讲 ionic3 compon

本文链接:https://www.haomeiwen.com/subject/etntdxtx.html