ionic2中使用自動(dòng)生成器的方法
ionic generator是命令行的功能,ionic2自動(dòng)幫我們創(chuàng)建應(yīng)用程序,從而節(jié)省了大量的時(shí)間,并增加我們的速度來(lái)開(kāi)發(fā)一個(gè)項(xiàng)目的關(guān)鍵部分。
ionic generator使我們可以自動(dòng)創(chuàng)建以下幾部份:
•component
•directive
•page
•provider
一、創(chuàng)建頁(yè)面:ionic g page [PageName]
通過(guò)這個(gè)命令創(chuàng)建一個(gè)新的頁(yè)面,ionic2項(xiàng)目中這個(gè)命令使用最多
我們只需要進(jìn)入我們的命令行中,并運(yùn)行下面的命令:
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>
二、創(chuàng)建組件:ionic g component [ComponentName]
組件是一段代碼,可以在我們的應(yīng)用程序的任何部分使用
通過(guò)這個(gè)命令創(chuàng)建一個(gè)組件:
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';
}
}
三、創(chuàng)建指令:ionic g directive [DirectiveName]
指令,我們的應(yīng)用程序可以在任何元素上使用的修飾符屬性.
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');
}
}
四、創(chuàng)建服務(wù)提供者:ionic g provider [ProviderName]
現(xiàn)在創(chuàng)建一個(gè)新的服務(wù)(提供者),提供者負(fù)責(zé)處理數(shù)據(jù)的REST API的連接,本地存儲(chǔ),SQLite的等等。
要?jiǎng)?chuàng)建它,我們?nèi)ミ\(yùn)行以下命令我們的終端:
ionic g provider userService # Results: √ Create app/providers/user-service/user-service.ts
服務(wù)代碼如下:
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);
});
});
}
}
五、創(chuàng)建管道pipe:ionic g pipe [PipeName]
該管道的變化,我們可以對(duì)任何數(shù)據(jù)使用我們的模板,如以大寫(xiě)字母顯示文本,顯示貨幣值,日期格式等。
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();
}
}
最后,我們生成的應(yīng)用程序結(jié)構(gòu)如下圖:

我們的項(xiàng)目將存放在一個(gè)更加有序和更多的控制方式,這一切都可以手動(dòng)實(shí)現(xiàn),但用ionic generator來(lái)做,可以節(jié)省寶貴的時(shí)間來(lái)創(chuàng)造這些內(nèi)容。
總結(jié)
以上所述是小編給大家介紹的ionic2中使用自動(dòng)生成器的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
使用自定義setTimeout和setInterval使之可以傳遞參數(shù)和對(duì)象參數(shù)
該函數(shù)兼容ie,firefox。并且可以使用clearSetTimeOut和clearInterval清除,比原setTimeout,setInterval方便很多,并且參數(shù)可以是object。2009-04-04
javascript 數(shù)組排序函數(shù)sort和reverse使用介紹
reverse方法將一個(gè)Array對(duì)象中的元素位置進(jìn)行反轉(zhuǎn),sort方法返回一個(gè)元素已經(jīng)進(jìn)行了排序的 Array 對(duì)象,下面為大家介紹下2013-11-11
小程序從手動(dòng)埋點(diǎn)到自動(dòng)埋點(diǎn)的實(shí)現(xiàn)方法
這篇文章主要介紹了小程序從手動(dòng)埋點(diǎn)到自動(dòng)埋點(diǎn)的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
layui前端框架之table表數(shù)據(jù)的刷新方法
今天小編就為大家分享一篇layui前端框架之table表數(shù)據(jù)的刷新方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
JS判斷兩個(gè)對(duì)象內(nèi)容是否相等的方法示例
這篇文章主要介紹了JS判斷兩個(gè)對(duì)象內(nèi)容是否相等的方法,結(jié)合具體實(shí)例形式分析了javascript針對(duì)字符串、數(shù)組及對(duì)象的相關(guān)判斷技巧,需要的朋友可以參考下2017-04-04
JavaScript設(shè)計(jì)模式之代理模式實(shí)例分析
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之代理模式,簡(jiǎn)單描述了代理模式的概念、原理并結(jié)合實(shí)例形式分析了javascript代理模式的相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2019-01-01
解決html input驗(yàn)證只能輸入數(shù)字,不能輸入其他的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決html input驗(yàn)證只能輸入數(shù)字,不能輸入其他的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
JS實(shí)現(xiàn)漂亮的時(shí)間選擇框效果
這篇文章主要介紹了JS實(shí)現(xiàn)漂亮的時(shí)間選擇框效果,結(jié)合實(shí)例形式分析了javascript時(shí)間選擇框插件的實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2016-08-08

