ionic2 generate生成器的使用

在開(kāi)發(fā)ionic2項(xiàng)目時(shí),會(huì)有很多頁(yè)面和服務(wù)需要?jiǎng)?chuàng)建,如果全靠自己手動(dòng)創(chuàng)建的話,會(huì)有很多重復(fù)的枯燥的操作要做,即增加開(kāi)發(fā)時(shí)間,又影響開(kāi)發(fā)效率,嚴(yán)重影響寫代碼的心情。

Ionic現(xiàn)在提供了一個(gè)生成器功能來(lái)為您的應(yīng)用程序創(chuàng)建頁(yè)面和服務(wù)。 這是一個(gè)命令行的功能,在終端輸入一個(gè)命令,告訴ionic你想創(chuàng)建什么,然后按下回車享受三秒閑暇時(shí)光:)

能夠創(chuàng)建的部分(CLI v2.2.3):

  • page
#command
ionic g page mypage
#result
√ Create app/pages/mypage/mypage.html 
√ Create app/pages/mypage/mypage.scss 
√ Create app/pages/mypage/mypage.ts

mypage.html

<!--
  Generated template for the Mypage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
-->
<ion-header>
  <ion-navbar>
    <ion-title>mypage</ion-title>
  </ion-navbar>
</ion-header>
<ion-content padding>
</ion-content>

mypage.ts

import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
/*
  Generated class for the Mypage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Component({
  selector: 'page-mypage',
  templateUrl: 'mypage.html'
})
export class MypagePage {
  constructor(public navCtrl: NavController, public navParams: NavParams) {}
  ionViewDidLoad() {
    console.log('ionViewDidLoad MypagePage');
  }
}
  • component
#command
ionic g component myComponent
#result
√ Create app/component/my-component/my-component.html 
√ Create app/component/my-component/my-component.scss 
√ Create app/component/my-component/my-component.ts 

my-component.ts

import { Component } from '@angular/core';
/*
  Generated class for the MyComponent component.
  See https://angular.io/docs/ts/latest/api/core/index/ComponentMetadata-class.html
  for more info on Angular 2 Components.
*/
@Component({
  selector: 'my-component',
  templateUrl: 'my-component.html'
})
export class MyComponentComponent {
  text: string;
  constructor() {
    console.log('Hello MyComponent Component');
    this.text = 'Hello World';
  }
}
  • directive
#command
ionic g directive myDirective
#result
√ Create app/components/my-directive/my-directive.ts

my-directive.ts

import { Directive } from '@angular/core';
/*
  Generated class for the MyDirective directive.
  See https://angular.io/docs/ts/latest/api/core/index/DirectiveMetadata-class.html
  for more info on Angular 2 Directives.
*/
@Directive({
  selector: '[my-directive]' // Attribute selector
})
export class MyDirective {
  constructor() {
    console.log('Hello MyDirective Directive');
  }
}
  • provider
#command
ionic g provider myProvider
#result
√ Create app/providers/my-provider.ts

my-provider.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
  Generated class for the MyProvider provider.
  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MyProvider {
  constructor(public http: Http) {
    console.log('Hello MyProvider Provider');
  }
}

以上四種形式是開(kāi)發(fā)過(guò)程中最長(zhǎng)用到的,在CLI>=3.0.0的版本中,ionic2新增了更多的服務(wù)生成器,用法基本一樣,詳見(jiàn)這里~

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

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

  • Ionic是一個(gè)基于Angular2的開(kāi)發(fā)手機(jī)web app的框架,它包含了一整套手機(jī)端的樣式組件,和一系列的功能...
    王兆龍閱讀 1,279評(píng)論 1 1
  • 本文使用Ionic2從頭建立一個(gè)簡(jiǎn)單的Todo應(yīng)用,讓用戶可以做以下事情: 查看todo列表 添加新的todo項(xiàng) ...
    孫亖閱讀 8,664評(píng)論 13 29
  • 0 開(kāi)始之前 通過(guò)本教程之前,您應(yīng)該至少了解一些基本的Ionic 2概念。您還必須已經(jīng)安裝了Ionic 2 在您的...
    孫亖閱讀 1,695評(píng)論 2 10
  • 畢業(yè)實(shí)習(xí)期的時(shí)候,老板是一對(duì)三十多歲的夫妻搭檔,男的要年長(zhǎng)女老板十歲左右,女老板沒(méi)有生育,所以女老板的性格非常火爆...
    半月散人閱讀 554評(píng)論 4 3
  • 春回大地,燕還溫巢 花情柳意,暉耀明陽(yáng) 鄉(xiāng)音繞渺,何不歸來(lái) 吾心羈絆,諸世繁華 蛟龍入海,無(wú)浪何歡 鷹翱太虛,豈戀...
    青春走過(guò)歲月閱讀 328評(píng)論 1 1

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