随笔 - 657,  文章 - 0,  评论 - 116,  阅读 - 153万

场景

Ionic介绍以及搭建环境、新建和运行项目:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106308166

在上面搭建起Ionic项目后 。

Ionic创建页面以及页面之间跳转、页面添加返回按钮、新增底部页面:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/106366142

实现页面的跳转后。

怎样把公共的功能抽离出来成为一个模块,然后在其他模块中引入这个公共的模块?

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

新建公共模块以及组件

ionic g module module/list
ionic g component module/list

 

 

公共模块list.module.ts中暴露对应的组件

复制代码
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListComponent } from './list.component';


@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ],
  exports:[ListComponent]
})
export class ListModule { }
复制代码

 

 

用到的地方引入自定义模块,并依赖注入自定义模块

这里在tab2.module.ts中引入

复制代码
import { IonicModule } from '@ionic/angular';
import { RouterModule } from '@angular/router';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Tab2Page } from './tab2.page';
import { ExploreContainerComponentModule } from '../explore-container/explore-container.module';

import { Tab2PageRoutingModule } from './tab2-routing.module';
import { ListModule } from '../module/list/list.module';
@NgModule({
  imports: [
    IonicModule,
    CommonModule,
    FormsModule,
    ExploreContainerComponentModule,
    Tab2PageRoutingModule,
    ListModule
  ],
  declarations: [Tab2Page]
})
export class Tab2PageModule {}
复制代码

 

 

使用自定义模块

为了能更好辨别是使用的公共模块在公共模块list.component.html中修改为

<ul>
  <li>霸道</li>
  <li>流氓</li>
  <li>气质</li>
</ul>

然后在tab2.page.html中使用公共模块

<app-list></app-list>

 

 

效果

 

 

Ionic自定义模块中使用内置模块

Ionic的内置模块

http://www.ionic.wang/components_doc-index.html

 

 

只需要在自定义公共模块这里是list.module.ts中引入并声明Ionic内置模块

复制代码
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ListComponent } from './list.component';
import { IonicModule } from '@ionic/angular';

@NgModule({
  declarations: [],
  imports: [
    CommonModule,
    IonicModule
  ],
  exports:[ListComponent]
})
export class ListModule { }
复制代码

 

 

然后就可以在自定义模块这里是list.component.html中使用ionic的内置模块了

复制代码
<ion-list>
  <ion-item>
    <ion-label>公众号:</ion-label>
  </ion-item>
  <ion-item>
    <ion-label>霸道的程序猿</ion-label>
  </ion-item>
  <ion-item>
    <ion-label>The Legend of Zeldaion-label></ion-label>
  </ion-item>
  <ion-item>
    <ion-label>Pac-Manion-label></ion-label>
  </ion-item>
  <ion-item>
    <ion-label>Super Mario Worldion-label></ion-label>
  </ion-item>
</ion-list>
复制代码

 

效果

 

 

posted on   霸道流氓  阅读(462)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示