ionic2 学习笔记

准备工作

官网地址:
http://ionicframework.com/docs/v2/components/#overview

官网为ionic2的学习者准备了Demo,如果事先装好了ionic2环境,仅需以下几个命令:

git clone git@github.com:driftyco/ionic-preview-app.git
cd ionic-preview-app
npm install
ionic serve

Tips:

  1. mac上如果出现acess相关问题,需在相关操作前加sudo
  2. 如果在npm install操作停留过久,可考虑使用cnpm

一切顺利的话,就能够看到Demo在浏览器上跑起来了,如图



</br>

正文

ActionSheet

ActionSheet效果图

代码位于<code>ionic-preview-app/app/pages/action-sheets/alerts/basic</code>

openMenu() {
    let actionSheet = ActionSheet.create({
      title: 'Albums',
      cssClass: 'action-sheets-basic-page',
      buttons: [
        {
          text: 'Delete',
          role: 'destructive',
          icon: !this.platform.is('ios') ? 'trash' : null,
          handler: () => {
            console.log('Delete clicked');
          }
        },
        {
          text: 'Share',
          icon: !this.platform.is('ios') ? 'share' : null,
          handler: () => {
            console.log('Share clicked');
          }
        },
        {
          text: 'Play',
          icon: !this.platform.is('ios') ? 'arrow-dropright-circle' : null,
          handler: () => {
            console.log('Play clicked');
          }
        },
        {
          text: 'Favorite',
          icon: !this.platform.is('ios') ? 'heart-outline' : null,
          handler: () => {
            console.log('Favorite clicked');
          }
        },
        {
          text: 'Cancel',
          role: 'cancel', // will always sort to be on the bottom
          icon: !this.platform.is('ios') ? 'close' : null,
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });

    this.nav.present(actionSheet);
  }

Tips:

  1. <code>button</code>中的<code>handler</code>处理与应用程序的交互,如果<code>handler</code>返回<code>false</code>,ActionSheet将不会消失。
  2. <code>role</code>为<code>Cancle</code>的<code>button</code>不管设置在哪个位置都会显示在底部。官方建议<code>role</code>为<code>destructive</code>的按钮最好处在第一个位置。

</br>

Alert

Alert basic
doAlert() {
    let alert = Alert.create({
      title: 'New Friend!',
      message: 'Your friend, Obi wan Kenobi, just approved your friend request!',
      buttons: ['Ok']
    });
    this.nav.present(alert);
  }

</br>


Alert checkbox
doCheckbox() {
    let alert = Alert.create();
    alert.setTitle('Which planets have you visited?');

    alert.addInput({
      type: 'checkbox',
      label: 'Alderaan',
      value: 'value1',
      checked: true
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Bespin',
      value: 'value2'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Coruscant',
      value: 'value3'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Endor',
      value: 'value4'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Hoth',
      value: 'value5'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Jakku',
      value: 'value6'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Naboo',
      value: 'value6'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Takodana',
      value: 'value6'
    });

    alert.addInput({
      type: 'checkbox',
      label: 'Tatooine',
      value: 'value6'
    });

    alert.addButton('Cancel');
    alert.addButton({
      text: 'Okay',
      handler: data => {
        console.log('Checkbox data:', data);
        this.testCheckboxOpen = false;
        this.testCheckboxResult = data;
      }
    });

    this.nav.present(alert).then(() => {
      this.testCheckboxOpen = true;
    });
 }

</br>


Alert confirm
doConfirm() {
    let confirm = Alert.create({
      title: 'Use this lightsaber?',
      message: 'Do you agree to use this lightsaber to do good across the intergalactic galaxy?',
      buttons: [
        {
          text: 'Disagree',
          handler: () => {
            console.log('Disagree clicked');
          }
        },
        {
          text: 'Agree',
          handler: () => {
            console.log('Agree clicked');
          }
        }
      ]
    });
    this.nav.present(confirm);
  }

</br>


Alert prompt
doPrompt() {
    let prompt = Alert.create({
      title: 'Login',
      message: "Enter a name for this new album you're so keen on adding",
      inputs: [
        {
          name: 'title',
          placeholder: 'Title'
        },
      ],
      buttons: [
        {
          text: 'Cancel',
          handler: data => {
            console.log('Cancel clicked');
          }
        },
        {
          text: 'Save',
          handler: data => {
            console.log('Saved clicked');
          }
        }
      ]
    });
    this.nav.present(prompt);
  }

</br>


Alert redio
doRadio() {
    let alert = Alert.create();
    alert.setTitle('Lightsaber color');

    alert.addInput({
      type: 'radio',
      label: 'Blue',
      value: 'blue',
      checked: true
    });

    alert.addInput({
      type: 'radio',
      label: 'Green',
      value: 'green'
    });

    alert.addInput({
      type: 'radio',
      label: 'Red',
      value: 'red'
    });

    alert.addInput({
      type: 'radio',
      label: 'Yellow',
      value: 'yellow'
    });

    alert.addInput({
      type: 'radio',
      label: 'Purple',
      value: 'purple'
    });

    alert.addInput({
      type: 'radio',
      label: 'White',
      value: 'white'
    });

    alert.addInput({
      type: 'radio',
      label: 'Black',
      value: 'black'
    });

    alert.addButton('Cancel');
    alert.addButton({
      text: 'Ok',
      handler: data => {
        console.log('Radio data:', data);
        this.testRadioOpen = false;
        this.testRadioResult = data;
      }
    });

    this.nav.present(alert).then(() => {
      this.testRadioOpen = true;
    });
  }

Tips:

  1. <code>input</code>选项:
    • type :input的类型
    • placeholder:占位符
    • value:checkbox的值
    • label:checkbox显示的文字
    • checked:是否选中

</br>

Bages

bages 效果图
<ion-item>
      <ion-icon name='musical-notes' item-left style="color: #d03e84"></ion-icon>
      Albums
      <ion-badge item-right>9</ion-badge>
    </ion-item>

    <ion-item>
      <ion-icon name='logo-twitter' item-left style="color: #55acee"></ion-icon>
      Followers
    <ion-badge item-right>260k</ion-badge>
</ion-item>

Tips:

  1. bages与上面交互所用方法不同,它更作用于界面的显示,形式也更像是ionic标签,代码也并非位于.ts文件而是.html文件中。
  2. 通常用于数值的显示。

</br>

Buttons

button basic

Tips:

  1. <code>button</code>的设置(界面部分)和badges和一样,也是写在.html里,形式如<code><button light>LIGHT</button></code>.
  2. 如上图,button的颜色属性分别为<code>light</code>,<code>secondary</code>,<code>danger</code>,<code>dark</code>.不写任何属性即为默认<code>default</code>

</br>

button block

Tips:

  1. <code>block</code>表示一个填充其父容器的小圆角按钮
  2. 代码格式形如:<code><button light block>LIGHT</button></code>.

</br>

button clear

Tips:

  1. <code>clear</code>表示一个不带边框的透明按钮。
  2. 代码格式形如:<code><button light clear>LIGHT</button></code>.

</br>

button in components

Tips:

  1. 表示在组件中的button,即与其他内容(如text,icon组合起来的button)。
  2. 格式如下:
<ion-item>
      Left Icon Button
      <button item-right outline>
        <ion-icon name='star'></ion-icon>
        Left Icon
      </button>
    </ion-item>

</br>

button fab

Tips:

  1. <code>fab</code>表示一个浮动的按钮。
  2. 代码格式形如:
<button fab secondary fab-top fab-center>
    <ion-icon name="compass" is-active="false">
    </ion-icon>
</button>

fab-left/fab-right/fab-center/fab-top/fab-bottom
控制浮动按钮的位置

</br>

button full

Tips:

  1. <code>full</code>表示一个填充其父容器的直角按钮
  2. 代码格式形如:<code>button light full>Light</button></code>

</br>

button icons

Tips:

  1. 表示可以和图标组合起来的button
  2. 代码形如:
<button light>
      <ion-icon name='arrow-back'></ion-icon>
      Back
</button>

</br>

button outline

Tips:

  1. <code>outline</code>表示一个带有边框的透明按钮。
  2. 代码形如:<code><button light outline>Light</button></code>

</br>

button round

Tips:

  1. <code>round</code>表示一个大圆角的按钮。
  2. 代码形如:<code><button light round>Light</button></code>

</br>

button size

Tips:

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

推荐阅读更多精彩内容