build your first app

Now that you have Ionic and its dependencies installed, you can build your first app! This section will guide you through the process of starting a new application, adding pages, navigating between those pages, and more. Let’s get started!

Starting a New Ionic 2 App

Let’s create a new app! Use the start command to initialize a new Ionic app. Specify that you want it to be an Ionic 2 app by passing in the –v2 option. We also specify that the tutorial template should be used. Ionic 2 applications are created as TypeScript.

$ ionic start MyIonic2Project tutorial --v2

This will pull Ionic 2 down, install npm modules for the application, and get Cordova set up and ready to go.

Viewing the app in a browser

$ cd MyIonic2Project/
$ ionic serve

Project Structure

Let’s walk through the anatomy of an Ionic 2 app. Inside of the folder that was created, we have a typical Cordova project structure where we can install native plugins, and create platform-specific project files.

./www/index.html

www/index.html is the main entry point for the app, though its purpose is to set up script and CSS includes and bootstrap, or start running, our app. We won’t spend much of our time in this file.

For your app to function, Ionic looks for the tag in your HTML. In this example we have:

<ion-app></ion-app>

And the following scripts near the bottom:

<script src="cordova.js"></script>
<script src="build/js/app.bundle.js"></script>

./app/app.ts

Inside of the app directory we find our pre-compiled code. This is where most of the work for an Ionic 2 app will take place. When we run ionic serve, our code inside of app/ is transpiled into the correct Javascript version that the browser understands (currently, ES5). That means we can work at a higher level using TypeScript and ES6+, but compile down to the older form of Javascript the browser needs.

app/app.ts is the entry point for our app.

Near the top of the file, we should see this:

@Component({
templateUrl: 'build/app.html'
})
class MyApp {
constructor() {
}
}

ionicBootstrap(MyApp)

Every app has a root component that essentially controls the rest of the application. This is very similar to ng-app from Ionic and Angular 1. This is also where we bootstrap our app using ionicBootstrap.

In this component, we set the template to be the file at build/app.html, which is a compiled version of app/app.html, let’s take a look!

./app/app.html

Here’s the main template for the app in app/app.html:

<ion-menu [content]="content">

  <ion-toolbar>
<ion-title>Pages</ion-title>
</ion-toolbar> <ion-content>
<ion-list>
<button ion-item *ngFor="let p of pages" (click)="openPage(p)">
{{p.title}}
</button>
</ion-list>
</ion-content> </ion-menu> <ion-nav id="nav" [root]="rootPage" #content swipe-back-enabled="false"></ion-nav>

In this template, we set up an ion-menu to function as a side menu, and then an ion-nav component to act as the main content area. The ion-menu’s [content] property is bound to the local variable content from our ion-nav, so it knows where it should animate around.

Adding Pages

Now that we have a basic understanding of the layout of an Ionic 2 app, let’s walk through the process of creating and navigating to pages in our app.

Taking a look at app/app.html, we see this line near the bottom:

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

Pay attention to the [root] property binding. This sets what is essentially the first, or “root” page for the ion-nav component. When ion-nav loads, the component referenced by the variable rootPage will be the root page.

In app/app.ts, the MyApp component specifies this in its constructor:

...
import {HelloIonicPage} from './pages/hello-ionic/hello-ionic';
... class MyApp {
... constructor(app, platform, menu) {
... // make HelloIonicPage the root (or first) page
this.rootPage = HelloIonicPage;
} ...
}

Creating a Page

Next, let’s check out the HelloIonicPage that we are importing. Inside the app/pages/hello-ionic/ folder, go and open up hello-ionic.ts.

You may have noticed that each page has its own folder that is named after the page. Inside each folder, we also see a .html and a .scss file with the same name. For example, inside of hello-ionic/ we will find hello-ionic.ts, hello-ionic.html, and hello-ionic.scss. Although using this pattern is not required, it can be helpful to keep things organized.

Below, we see the HelloIonicPage class. This creates a Page - an Angular component with all Ionic directives already provided, to be loaded using Ionic’s navigation system. Notice that because Pages are meant to be loaded dynamically, they don’t have a selector:

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

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

All pages have both a class, and an associated template that’s being compiled as well. Let’s check out app/pages/hello-ionic/hello-ionic.html - the template file for this page:

<ion-navbar *navbar>
<button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>Hello Ionic</ion-title>
</ion-navbar> <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>

The

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 {
// provide Angular with metadata about things it should inject in the constructor
static get parameters() {
return [[NavController], [NavParams]];
} constructor(nav, navParams) {
this.nav = nav; // 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.nav.push(ItemDetailsPage, {
item: item
});
}
}

This page will create a basic list page containing a number of items.

Navigating to Pages

Recall from the previous section we had a function inside our ListPage class that looked something like this:

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

You might have noticed we are referencing ItemDetailsPage. This is a page included in the tutorial starter. Let’s import it in app/pages/list/list.js so we can use it:

...
import {ItemDetailsPage} from '../item-details/item-details';

After saving the file, you will notice the ionic serve process will recompile your app with the new changes, and reload the browser. Let’s revisit our app in the browser, and when we tap an item, it will navigate to the item details page! Notice that the menu-toggle is replaced with a back button instead. This is a native style that Ionic follows, but can be configured.

How It Works

Navigation in Ionic 2 works like a simple stack, where we push new pages onto the top of the stack, which takes us forwards in the app and shows a back button. To go backwards, we pop the top page off. Since we set this.nav in the constructor, we can call this.nav.push(), and pass it the page we want to navigate to. We can also pass it an object containing data we would like to pass to the page being navigated to. Using push to navigate to a new page is simple, but Ionic’s navigation system is very flexible. Check out the navigation docs to see more advanced navigation examples.

When it comes to URLs, Ionic 2 works a bit differently than Ionic 1. Instead of using URLs to navigate, we use them to make sure we can always come back to a page (on app launch, for example). This means we aren’t limited to using href to navigate around. However, we still have the option to use a URL to navigate to a page when necessary.

references

Tutorial - Getting Started with Ionic - Ionic Framework

effect

Ionic2 Tutorial的更多相关文章

  1. 浅谈angular2+ionic2

    浅谈angular2+ionic2   前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别.   1. 项目所用:angular2+ionic2 ...

  2. ionic2配置问题集

    我在配置ionic2中遇到的问题,及我的解决方案. ionic start MyIonic2Project tutorial --v2 网络很坑爹,配置lantern后,也是多次尝试才下载成功. io ...

  3. ionic2新手入门整理,搭建环境,创建demo,打包apk,热更新,优化启动慢等避坑详解

    onic官方文档链接:http://ionicframework.com/docs/ 如果是新的环境会有很多坑,主要是有墙,请仔细阅读每个步骤 文档包含以下内容: l  环境搭建 l  创建demo并 ...

  4. Ionic2+ 环境搭建

    ionic2+官方guide 基础环境安装 nodejs安装 ionic,cordova安装 npm install -g ionic cordova 项目创建 ionic start MyIonic ...

  5. angular2+ionic2架构介绍

    不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别.   1. 项目所用:angular2+ionic2+typescript 2. 项目结构 3. S ...

  6. Ionic2开发环境搭建、项目创建调试与Android应用的打包、优化

    Ionic2开发环境搭建.项目创建调试与Android应用的打包.优化. windows下ionic2开发环境配置步骤如下: 下载node.js环境,稳定版本:v6.9.5 下载android stu ...

  7. Ionic2开发环境搭建

    关于网络环境:ionic开发环境不需要FQ.我这里没有设置FQ,亲测可行.但是angular2的开发环境搭建,则需要FQ网络,否则很多包会安装失败. 建议大家在搭建开发环境的时候,不要参考百度出来的各 ...

  8. Ionic2 自学须知的基本知识点

    http://www.cnblogs.com/zsl123/p/5991336.html Ionic(ionicframework)一款接近原生的HTML5移动App开发框架. IONIC 是目前最有 ...

  9. 浅谈Ionic2

    http://www.cnblogs.com/zhouming-web/p/6226323.html 前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScrip ...

随机推荐

  1. IntelliJ Idea 修改编码格式

    Setting→Editor→File Encodings→设置“Project Encoding”为UTF-8,如图:

  2. HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...

  3. c++ 操作符重载和友元

    操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...

  4. [转载]localhost与127.0.0.1的区别

    原文链接:http://blog.csdn.net/xifeijian/article/details/12879395 很多人会接触到这个ip地址127.0.0.1.也许你会问127.0.0.1是什 ...

  5. Laravel5.1-错误和日志

    简介 这一章也是属于文档写得比较混乱的一章,所以我决定重新组织一下内容结构: 配置 debug配置 我们都知道,开发环境应该把debug打开,生产环境应该把debug关闭:这个设置在config/ap ...

  6. 跟着百度学PHP[4]OOP面对对象编程-17-多态

    多态是除封装和继承之外的另一个面象对象的三大特性之一. 多态的作用简而言之就是为程序做括展. 比如说在公司上班,每个月财务发放工资,同一个发工资的方法,在公司内不同的员工或是不同职位的员工,都是通过这 ...

  7. 【VBA】批量插入图片

    解决如下问题: 需要批量导入图片到Excel 图片放在一个文件夹中 图片有严格的顺序关系,即按照:共通名_编号的方式命名. 图片格式统一,即均为同一格式. 有两种方式可以插入图片到Excel中,其一为 ...

  8. python——tuple元组

    >>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', ...

  9. python日志浅析

    输出日志对于追踪问题比较重要. 默认logger(root) python使用logging模块来处理日志.通常下面的用法就能满足常规需求: import logging logging.debug( ...

  10. nginx配置多域名映射方法(本地hosts)

    本地测试网站的时候如果不想用localhost/xxxx的形式访问,可能就需要修改hosts文件来映射了,但是一个网站还好,假如有多个网站的话就不行了. 这时就需要配置多域名映射 比如hosts中配置 ...