Ionic 2 添加頁面

現(xiàn)在我們已經(jīng)基本知道了Ionic2 app的布局,接下來我們來走一遍在我們的app里創(chuàng)建和導(dǎo)航頁面的過程。

先看看src/app/app.html, 接近底部的地方有如下內(nèi)容:

<ion-nav id="nav" [root]="rootPage" #content swipeBackEnabled="false"></ion-nav>

注意[root]屬性綁定。設(shè)置了ion-nav組件的根頁面或是第一個基本頁面。當(dāng)加載ion-nav是,rootPage變量引用的就是根頁面。

在 src/app/app.component.ts 里, MyApp 組件在它的構(gòu)造器中定義了他。:

...
import {HelloIonicPage} from '../pages/hello-ionic/hello-ionic';
...

export class MyApp {
  ...
  
  // make HelloIonicPage the root (or first) page
  rootPage: any = HelloIonicPage;
  pages: Array<{title: string, component: any}>;

    constructor(
      private platform: Platform,
      private menu: MenuController
    ) {
    ...
  }

  ...
}

我們可以看到rootPage設(shè)置為HelloIonicPage,因此HelloIonicPage將會是nav controller中加載的第一個頁面。讓我們來看一下。

創(chuàng)建頁面

接下來我們看看導(dǎo)入的HelloIonicPage 。在 src/pages/hello-ionic/目錄下,打開hello-ionic.ts文件。

你可能注意到每個頁面有一個目錄。在每個目錄中還有另外兩個同名的.html 和 .scss 文件。例如,在hello-ionic/里面有hello-ionic.ts, hello-ionic.html 和 hello-ionic.scss三個文件。盡管這不是必須的模式,但是這對組織代碼很有幫助。

下面,我們看到HelloIonicPage類。這將創(chuàng)建一個頁面,提供一個包含所有Ionic指令的Angular組件,加載使用Ionic的導(dǎo)航系統(tǒng)。請注意,因為頁面是動態(tài)加載,他們沒有選擇器:

import {Component} from '@angular/core';

@Component({
  templateUrl: 'build/pages/hello-ionic/hello-ionic.html'
})
export class HelloIonicPage {}

所有頁面都有一個類,和一個關(guān)聯(lián)的模板的編譯。 我們看看 src/pages/hello-ionic/hello-ionic.html - 這個頁面的模版文件:

<ion-header>
  <ion-navbar>
    <button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Hello Ionic</ion-title>
  </ion-navbar>
</ion-header>


<ion-content padding class="getting-started">

  <h3>Welcome to your first Ionic app!</h3>

  <p>
    This starter project is our way of helping you get a functional app running in record time.
  </p>
  <p>
    Follow along on the tutorial section of the Ionic docs!
  </p>
  <p>
    <button primary menuToggle>Toggle Menu</button>
  </p>

</ion-content>

<ion-navbar>是這個頁面的導(dǎo)航條模版。當(dāng)我們導(dǎo)航到這個頁面,導(dǎo)航條上的按鈕和標(biāo)題作為頁面的一部分一起過渡過來。
余下的模版是標(biāo)準(zhǔn)的Ionic代碼設(shè)置內(nèi)容區(qū)域,打印歡迎信息。

創(chuàng)建附加頁面

創(chuàng)建附加頁面,我們只需要確保正確設(shè)置標(biāo)題和其他我們希望導(dǎo)航條顯示的東西。
我們再來看看src/pages/list/list.ts里面的內(nèi)容,你會發(fā)現(xiàn)定義了一個新的頁面:

import {Component} from "@angular/core";
import {NavController, NavParams} from 'ionic-angular';
import {ItemDetailsPage} from '../item-details/item-details';


@Component({
  templateUrl: 'build/pages/list/list.html'
})
export class ListPage {
  selectedItem: any;
  icons: string[];
  items: Array<{title: string, note: string, icon: string}>;

  constructor(private navCtrl: NavController, navParams: NavParams) {
    // If we navigated to this page, we will have an item available as a nav param
    this.selectedItem = navParams.get('item');

    this.icons = ['flask', 'wifi', 'beer', 'football', 'basketball', 'paper-plane',
    'american-football', 'boat', 'bluetooth', 'build'];

    this.items = [];
    for(let i = 1; i < 11; i++) {
      this.items.push({
        title: 'Item ' + i,
        note: 'This is item #' + i,
        icon: this.icons[Math.floor(Math.random() * this.icons.length)]
      });
    }
  }

  itemTapped(event, item) {
     this.navCtrl.push(ItemDetailsPage, {
       item: item
     });
  }
}

這個頁面創(chuàng)建了一個包含多個數(shù)據(jù)項的列表頁。總之,這個頁面和前面的HelloIonicPage 很相似。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 0 開始之前 通過本教程之前,您應(yīng)該至少了解一些基本的Ionic 2概念。您還必須已經(jīng)安裝了Ionic 2 在您的...
    孫亖閱讀 1,695評論 2 10
  • 完成Ionic安裝后,你可以創(chuàng)建第一個App了。本章內(nèi)容將指導(dǎo)你新建一個App,添加一個頁面,并且實現(xiàn)頁面間的導(dǎo)航...
    全棧弄潮兒閱讀 500評論 0 2
  • ionic項目結(jié)構(gòu) 接下來我們就要通過分析ionic項目結(jié)構(gòu),從而正式進(jìn)入ionic項目中了,在此之前,大家得先掌...
    leezerofly閱讀 4,224評論 1 7
  • 本文使用Ionic2從頭建立一個簡單的Todo應(yīng)用,讓用戶可以做以下事情: 查看todo列表 添加新的todo項 ...
    孫亖閱讀 8,664評論 13 29
  • 與其講一大堆道理,不如講一個好故事。 最打動人心的還是故事 。 怎么樣去講好一個故事呢? 如果是你親身經(jīng)歷的,會特...
    胖燈閱讀 156評論 0 1

友情鏈接更多精彩內(nèi)容