ionic2中使用自動生成器-創(chuàng)新互聯(lián)

小編給大家分享一下ionic2中使用自動生成器,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站主營普洱網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都app軟件開發(fā)公司,普洱h5成都小程序開發(fā)搭建,普洱網(wǎng)站營銷推廣歡迎普洱等地區(qū)企業(yè)咨詢

ionic generator是命令行的功能,ionic2自動幫我們創(chuàng)建應用程序,從而節(jié)省了大量的時間,并增加我們的速度來開發(fā)一個項目的關鍵部分。

ionic generator使我們可以自動創(chuàng)建以下幾部份:

?component
?directive
?page
?provider

一、創(chuàng)建頁面:ionic g page [PageName]

通過這個命令創(chuàng)建一個新的頁面,ionic2項目中這個命令使用最多

我們只需要進入我們的命令行中,并運行下面的命令:

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]

組件是一段代碼,可以在我們的應用程序的任何部分使用

通過這個命令創(chuàng)建一個組件:

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]

指令,我們的應用程序可以在任何元素上使用的修飾符屬性.

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)建服務提供者:ionic g provider [ProviderName]

現(xiàn)在創(chuàng)建一個新的服務(提供者),提供者負責處理數(shù)據(jù)的REST API的連接,本地存儲,SQLite的等等。

要創(chuàng)建它,我們?nèi)ミ\行以下命令我們的終端:

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); 
  }); 
 }); 
 } 
}

五、創(chuàng)建管道pipe:ionic g pipe [PipeName]

該管道的變化,我們可以對任何數(shù)據(jù)使用我們的模板,如以大寫字母顯示文本,顯示貨幣值,日期格式等。

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(); 
 } 
}

最后,我們生成的應用程序結構如下圖:

ionic2中使用自動生成器

我們的項目將存放在一個更加有序和更多的控制方式,這一切都可以手動實現(xiàn),但用ionic generator來做,可以節(jié)省寶貴的時間來創(chuàng)造這些內(nèi)容。

以上是ionic2中使用自動生成器的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)成都網(wǎng)站設計公司行業(yè)資訊頻道!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享文章:ionic2中使用自動生成器-創(chuàng)新互聯(lián)
文章起源:http://bm7419.com/article22/hdscc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務網(wǎng)站改版、定制網(wǎng)站網(wǎng)頁設計公司、全網(wǎng)營銷推廣動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化