ionic2 generate生成器的使用

在开发ionic2项目时,会有很多页面和服务需要创建,如果全靠自己手动创建的话,会有很多重复的枯燥的操作要做,即增加开发时间,又影响开发效率,严重影响写代码的心情。

Ionic现在提供了一个生成器功能来为您的应用程序创建页面和服务。 这是一个命令行的功能,在终端输入一个命令,告诉ionic你想创建什么,然后按下回车享受三秒闲暇时光:)

能够创建的部分(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');
  }
}

以上四种形式是开发过程中最长用到的,在CLI>=3.0.0的版本中,ionic2新增了更多的服务生成器,用法基本一样,详见这里~

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Ionic是一个基于Angular2的开发手机web app的框架,它包含了一整套手机端的样式组件,和一系列的功能...
    王兆龙阅读 1,218评论 1 1
  • 本文使用Ionic2从头建立一个简单的Todo应用,让用户可以做以下事情: 查看todo列表 添加新的todo项 ...
    孙亖阅读 8,598评论 13 29
  • 0 开始之前 通过本教程之前,您应该至少了解一些基本的Ionic 2概念。您还必须已经安装了Ionic 2 在您的...
    孙亖阅读 1,672评论 2 10
  • 毕业实习期的时候,老板是一对三十多岁的夫妻搭档,男的要年长女老板十岁左右,女老板没有生育,所以女老板的性格非常火爆...
    c1dd5a05d5c8阅读 497评论 4 3
  • 春回大地,燕还温巢 花情柳意,晖耀明阳 乡音绕渺,何不归来 吾心羁绊,诸世繁华 蛟龙入海,无浪何欢 鹰翱太虚,岂恋...
    青春走过岁月阅读 279评论 1 1