ionic学习笔记

ionic 生命周期

  • ionViewDidLoad : 当页面加载的时候触发,仅在页面创建的时候触发一次,如果被缓存了,那么下次再打开这个页面则不会触发
  • ionViewWillEnter : 当将要进入页面时触发
  • ionViewDidEnter: 当已经进入页面时触发
  • ionViewWillLeave : 当将要离开页面时触发
  • ionViewDidLeave : 离开页面时触发
  • ionViewWillUnload : 当页面将要销毁同时页面上元素移除时触发

自定义插件调用时, 如果报线程警告:

THREAD WARNING: ['UdeskPlugin'] took '290.006104' ms. Plugin should use a background thread.

那么需要将原生里改为:

- (void)uDeskMethod:(CDVInvokedUrlCommand*)command {
     [self.commandDelegate runInBackground:^{
        dispatch_async(dispatch_get_main_queue(), ^{
        });
    }];
}

获取组件内封装的标签,如<ion-content>中封装的<scroll-content>,将其设置禁止滚动

```  
 <ion-content #ionContent id="aboutContent" ></ion-content>
方法一:
    /// querySelector() 方法返回文档中匹配指定 CSS 选择器的第一个元素。
  let aboutContent = document.querySelector("#aboutContent");
  let scroll: any = aboutContent.querySelector(".scroll-content");
  scroll.style.overflow = 'hidden'
 方法二:
 //  @ViewChild(Content) content: Content; 这也可以
     @ViewChild('ionContent') content
   // 可以通过打印 content 看到里面属性及方法
    this.content._scrollContent.nativeElement.style.overflow = 'hidden'

两种隐藏内容且不占空间的方法:

```
*ngIf : 彻底从页面中删除, 若页面其它地方有引用则会报错;
display: none :  从页面中隐藏, 若页面中其它地方要引用则用它;
```

JS中 on 开头的方法, 都可以用 () 来表示 : 如: on-click => (click); on-input => (input);

image.png

轮播图的自动滚动及Item 无法点击的问题:

<!--  HTLM  -->
<div class="remindContent">
          <!-- *ngIf="remindData && remindData.length >= 1":  这个必须要加,不然会报莫名的错误  -->
          <!-- 其中的属性去看官方文档  -->
          <ion-slides id="ionSlide" #ionSlides direction="vertical" effect="slide" autoplay="3000" speed="2000" loop="true"
                      *ngIf="remindData && remindData.length >= 1" (ionSlideTap)="ionSlideTapMethod()">
              <ion-slide *ngFor="let item of remindData" >
                  <div>
                      <custom-img name="home_notification" width="10" height="12"></custom-img>
                      <span>{{item.warnInfo}}</span>
                  </div>
              </ion-slide>
          </ion-slides>
          <div *ngIf="!remindData || remindData.length === 0">{{remindText}}</div>
</div>

<!-- ts文件  -->
  <!-- item的点击方法,  this.slides.realIndex 可以获取当前显示的item的下标 -->
  ionSlideTapMethod() {
      let slideIndex = this.slides.realIndex;
      this.warningOrderItem(this.remindData[slideIndex]);
  }

<!--  跳转离开页面再返回时, 自动轮播失败的解决方法  -->
   @ViewChild(Slides) slides: Slides;
   <!-- 进入页面开始滚动  -->
  ionViewDidEnter() {
      if (this.slides) {
          this.slides.startAutoplay();
      }
  }
  <!-- 离开页面停止滚动  -->
  ionViewWillLeave() {
      if (this.slides) {
          this.slides.stopAutoplay();
      }
  }

form表单的使用:

  /**
     *  要求: 每个<ion-input> 必须取个 name ; 必须加 required; <button>必须写在form表单内; 注意[disabled]的使用;
     */
<form action="" #signForm="ngForm" *ngIf="!selectBinding">
  <ion-grid>
    <ion-row>
      <ion-col col-3>
        <ion-label>支行</ion-label>
      </ion-col>
      <ion-col>
        <ion-input name="subBranchName" required placeholder="请输入" [(ngModel)]="bankModal.subBranchName"></ion-input>
      </ion-col>
    </ion-row>
    <ion-row>
      <ion-col col-3>
        <ion-label>持卡人</ion-label>
      </ion-col>
      <ion-col>
        <ion-input name="accountName" required placeholder="请输入"[(ngModel)]="bankModal.accountName"></ion-input>
      </ion-col>
    </ion-row>
</ion-grid>
<button ion-button type="submit" [disabled]="signForm.invalid || !bank" tappable
                    (click)="buttonClick()" class="confirmButton">确定修改
</button>
</form>

input标签自动获取焦点: autofocus="autofocus" , 监听键盘及隐藏( https://ionicframework.com/docs/api/platform/Keyboard/);

<!-- 官方文档里有很多键盘的方法 -->
import {Keyboard} from 'ionic-angular';
constructor(public keyboard: Keyboard) {}

this.keyboard.close();  // 键盘隐藏;

input输入框内容变化实时监听

onchange事件:
此事件会在元素内容发生改变,且失去焦点的时候触发。浏览器支持度较好。
onpropertychange事件:
此事件会在元素内容发生改变时立即触发,即便是通过js改变的内容也会触发此事件。元素的任何属性改变都会触发该事件,不止是value。只有IE11以下浏览器支持此事件。
oninput事件:
此事件会在value属性值发生改变时触发,通过js改变value属性值不会触发此事件。只有IE8以上或者谷歌火狐等标准浏览器支持。

<ion-input  type="text" placeholder="请输入" [(ngModel)]="identifyCode" (change)="valueChange()  name="input"></ion-input>

导航栏后退返回到指定页面:

image.png

数据的双向绑定, 获取 input 标签里的值

<!-- [abnormalType]: 属性绑定;  (filterConfirmBtn): 方法绑定; [(filterCondition)]: 双向绑定; -->
<filter-content [abnormalType]="bigType" (filterConfirmBtn)="filterConfirmBtnClick()" [(filterCondition)]="filterConditions"></filter-content>

<!-- ts中  -->
    @Input() abnormalType: any = [];
    @Output() filterConfirmBtn: EventEmitter<any> = new EventEmitter<any>();
    @Input() filterCondition: any = {};
    @Output() filterConditionChange: EventEmitter<any> = new EventEmitter<any>();
    
    // 绑定触发
    confirmClickMethod($event) {
        this.filterConditionChange.emit($event);
        this.filterConfirmBtn.emit($event);
    }

输入框的监听事件

 <!--  输入框的监听及限定字符的输入   -->
<div class="addMemoTextArea">
    <ion-textarea no-margin placeholder="请输入" maxlength="100" required name="abnormalText"
                          [(ngModel)]="textContent" (input)="inputEvent($event)"></ion-textarea>
</div>
  1. 抽屉效果的实现:


    image.png
image.png
image.png
image.png
image.png
image.png
image.png

注: iOS上因为弹簧效果 可能导致抽屉出来后页面还可以滚动, 所以要禁止页面的滚动

// 禁止背景内容滚动
    forbidContentScroll() {
        //获取当前组件的ID
        let aboutContent = document.querySelector("#depositeAuditContent");
        //获取当前组件下的scroll-content元素
        let scroll: any = aboutContent.querySelector(".scroll-content");

        if (!this.toggle) {
            scroll.style.overflow = 'hidden';
        } else {
            scroll.style.overflow = '';
        }
    }

拥有superTabs的页面pop()返回出现页面空白的解决办法:

 参考: https://github.com/zyra/ionic2-super-tabs/issues/29
          https://ionicframework.com/docs/native/native-page-transitions/
安装两个依赖: 
     $ ionic cordova plugin add com.telerik.plugins.nativepagetransitions
     $ npm install --save @ionic-native/native-page-transitions
image.png

image.png

页面内弹框效果的实现(效果如下图)

图片
HTLM: 
<button ion-button tappable (click)="accountExplainClick()">弹框按钮</button>
<!-- 封装的弹框 -->
<wallet-caller *ngIf="toggleAccountPanel" [clientState]="pageModul.state" [accountExplainPanel]="toggleAccountPanel"></wallet-caller>
TS:
    // 进页面注册方法
    ionViewWillEnter() {
        // 金额说明弹框
        window['epInstance'].unbind('toggle_accountExplain_panel').bind('toggle_accountExplain_panel', (data) => {
            if (data) {
                this.toggleAccountPanel = true;
            } else {
                this.toggleAccountPanel = false;
            }
        });
    }
    // 弹框按钮点击方法
    accountExplainClick() {
        window['epInstance'].emit('toggle_accountExplain_panel', "accountExplain");
    }

<wallet-caller> 中 css 的弹框样式
      .account-wrap {
            position: fixed;
            background-color: rgba(0, 0, 0, .3);
            z-index: 10;
            width: 100%;
            height: 100%;
        .remark {
            max-height: 75%;
            overflow-y: auto;
            width: 90%;
            position: absolute;
            top: 50%;
            left: 50%;
            border-radius: 10px;
            transform: translate(-50%, -50%);
            padding: 0px 18px 15px;
            z-index: 99;
        }
    }

// 关闭弹框的方法
  close() {
        window['epInstance'].emit('toggle_accountExplain_panel', null);
    }

遇到的一些报错:

Node Sass does not yet support your current environment解决办法
这个问题在升级ionic2的时候可能会遇到,是sass不支持当前的环境,那么在当前环境重新安装一下就好了

npm uninstall --save node-sass 

npm install --save node-sass

ionic 一些常用命令:

  • ionic cordova emulate ios --list 查看支持的设备

  • ionic lab 浏览器上运行

  • npm start 浏览器上运行(检查代码规范)

  • ionic cordova platform add ios 添加平台

  • ionic cordova platform rm ios 移除平台

  • ionic cordova emulate ios 模拟器运行

  • ionic cordova emulate ios --target="iPhone-6s-Plus" 指定模拟器运行 (通过 ionic cordova emulate ios --list 查看支持的模拟器)

  • npm install -g cnpm --registry=https://registry.npm.taobao.org(npm镜像源指向淘宝)

  • ionic g page xxxPage(创建页面)

  • ionic start myApp blank(空项目)

  • ionic start myApp tabs(带导航条)

  • ionic start myApp sidemenu(带侧滑菜单)

  • ionic cordova build ios (编译)

  • ionic cordova build ios --prod (打包用这个命令)

创建插件:

  • plugman create --name UdeskPlugin --plugin_id cordova-plugin-udeskPlugin --plugin_version 0.0.1 // 创建插件

  • plugman platform add --platform_name ios // 添加平台

  • sudo cordova plugin add 插件目录 // 添加插件

  • sudo cordova plugin remove cordova.plugin.UdeskPlugin // 移除插件

  • cordova plugin list // 查看插件列表

参考文章:

持续更新中.....

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,204评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,091评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,548评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,657评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,689评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,554评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,302评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,216评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,661评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,851评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,977评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,697评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,306评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,898评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,019评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,138评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,927评论 2 355

推荐阅读更多精彩内容