ionic2+Angular 组件(多个组件)浅谈

第一步,新建组件:

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即可。

 

转载于:https://www.cnblogs.com/tomboyxiao/p/7366009.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular是一个流行的JavaScript框架,用于构建Web应用程序。Ionic是基于Angular的开源移动应用开发框架,它提供了一套UI组件和具,帮助开发者构建跨平台的移动应用程序。 Swiper是一个流行的移动端滑动组件,它提供了丰富的滑动效果和交互功能,可以用于创建漂亮的轮播图、图片浏览器等。 结合AngularIonic,你可以轻松地集成Swiper组件到你的移动应用中。首先,你需要在你的Angular项目中安装Swiper组件。可以使用npm命令来安装: ``` npm install swiper --save``` 安装完成后,你可以在你的Ionic组件中引入Swiper组件,并在模板中使用它。以下是一个简单的示例: ```typescriptimport { Component } from '@angular/core'; import SwiperCore, { Navigation, Pagination } from 'swiper/core'; SwiperCore.use([Navigation, Pagination]); @Component({ selector: 'app-swiper', template: ` <swiper [navigation]="true" [pagination]="true"> <ng-template swiperSlide>Slide1</ng-template> <ng-template swiperSlide>Slide2</ng-template> <ng-template swiperSlide>Slide3</ng-template> </swiper> `, }) export class SwiperComponent {} ``` 在上面的示例中,我们首先引入了Swiper组件,并注册了所需的Swiper模块(例如Navigation和Pagination)。然后,在组件的模板中,我们使用`<swiper>`标签创建了一个Swiper实例,并在内部添加了三个滑动的内容块。 你可以根据你的需求自定义Swiper的配置和样式。更多关于Swiper的用法和配置,你可以参考Swiper官方文档。 希望这可以帮助到你!如果你还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值