标签:rgs res head new nav table promise const .text
ionic generator是命令行的功能,ionic2自动帮我们创建应用程序,从而节省了大量的时间,并增加我们的速度来开发一个项目的关键部分。
一、创建页面:ionic g page [PageName]
ionic g page login # Results: √ Create app/pages/login/login.html √ Create app/pages/login/login.scss √ Create app/pages/login/login.ts
login.ts:
import {Component} from ‘@angular/core‘; import {NavController} from ‘ionic-angular‘; @Component({ templateUrl: ‘build/pages/login/login.html‘, }) export class LoginPage { constructor(public nav: NavController) {} }
login.html:
<ion-header> <ion-navbar> <ion-title> login </ion-title> </ion-navbar> </ion-header> <ion-content padding class="login"> </ion-content>
二、创建组件:ionic g component [ComponentName]
ionic g component myComponent # Results: √ Create app/components/my-component/my-component.html √ Create app/components/my-component/my-component.ts
my-component.ts:
import {Component} from ‘@angular/core‘; @Component({ selector: ‘my-component‘, templateUrl: ‘build/components/my-component/my-component.html‘ }) export class MyComponent { text: string = ""; constructor() { this.text = ‘Hello World‘; } }
ionic g directive myDirective # Results: √ Create app/components/my-directive/my-directive.ts
my-directive.ts:
import {Directive} from ‘@angular/core‘; @Directive({ selector: ‘[my-directive]‘ // Attribute selector }) export class MyDirective { constructor() { console.log(‘Hello World‘); } }
四、创建服务提供者:ionic g provider [ProviderName]
ionic g provider userService # Results: √ Create app/providers/user-service/user-service.ts
服务代码如下:
user-service.ts:
import {Injectable} from ‘@angular/core‘; import {Http} from ‘@angular/http‘; import ‘rxjs/add/operator/map‘; @Injectable() export class UserService { data: any = null; constructor(public http: Http) { } load() { if (this.data) { } return new Promise(resolve => { this.http.get(‘path/to/data.json‘) .map(res => res.json()) .subscribe(data => { this.data = data; resolve(this.data); }); }); } }
五、创建管道pipe:ionic g pipe [PipeName]
ionic g pipe myPipe # Results: √ Create app/pipes/myPipe.ts
我们的管道的代码如下
myPipe.ts:
import {Injectable, Pipe} from ‘@angular/core‘; @Pipe({ name: ‘my-pipe‘ }) @Injectable() export class MyPipe { transform(value: string, args: any[]) { value = value + ‘‘; // make sure it‘s a string return value.toLowerCase(); } }
最后,我们生成的应用程序结构如下图:
标签:rgs res head new nav table promise const .text
原文地址:http://www.cnblogs.com/ckAng/p/7297758.html