Open uniquejava opened 7 years ago
如下定义Component.
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
}
用法: <page-home />
.
@Directive({
selector: '[my-selector]'
})
用法: <some-element my-selector />
@Pipe({
name: 'myPipe'
})
用法: {{someThing | myPipe}}
@Inejctable
export class DataService {
}
pipes, directives, injectables, components需要在app.module.ts
中declare.
@IonicPage
和@Component
一样, 但是让Component可以lazy load.
module文件: “src/pages/lazy-load/lazy-load.module.ts"
import { NgModule } from '@angular/core';
import { IonicModule } from 'ionic-angular';
import { LazyLoad } from './lazy-load';
@NgModule({
declarations: [
LazyLoad,
],
imports: [
IonicModule.forChild(LazyLoad),
],
exports: [
LazyLoad
]
})
export class LazyLoadModule {}
component文件: “src/pages/lazy-load/lazy-load.ts"
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
@IonicPage()
@Component({
selector: 'page-lazy-load',
templateUrl: 'lazy-load.html',
})
export class LazyLoad {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
console.log('ionViewDidLoad LazyLoad');
}
}
class Person {
constructor(name, age){
this.name = name;
}
setAge(age) {
this.age = age;
return this;
}
isOld(){...}
}
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
templateUrl: 'my-page.html',
})
// 如果在要ionic中使用service需要通过constructor inject进来.
export class MyPage {
someMemberVariable: any = 'hey!';
constructor(platform: Platform, nav: NavController) {
platform.ready().then(() => {});
}
ionViewDidLoad() {
//this runs once the view has loaded
}
someMethod(){
//this only runs when called
}
someOtherMethod(){
//this only runs when called
}
}
对应的模板文件
<ion-header>
<ion-navbar>
<ion-title>
My Page
</ion-title>
</ion-navbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item>I</ion-item>
<ion-item>Am</ion-item>
<ion-item>A</ion-item>
<ion-item>List</ion-item>
</ion-list>
</ion-content>
constructor(platform: Platform, nav: NavController) {
this.platform = platform;
}
可以简写成
constructor(public platform: Platform, nav: NavController) {
}
同创建Page, 只是selector是必选项.
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
templateUrl: 'my-component.html'
})
export class MyComponent {
text: any;
constructor() {
this.text = 'Hello World';
}
}
模板
<div>
{{text}}
</div>
在app.module.ts
中声明
@NgModule({
declarations: [
MyApp,
HomePage,
MyComponent
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: []
})
export class AppModule {}
最后可以在page template中这样使用自定义的component
<my-component></my-component>
“a directive is used to modify the behaviour of an existing element”
import { Directive, ElementRef } from '@angular/core';
“@Directive({
selector: '[my-directive]'
})
export class MyDirective {
constructor(element: ElementRef) {
this.element = element;
}
}
比如<button ion-button>
或<ion-list no-lines>
为什么要加@Injectable
不加会报什么错? (我发现用ionic g命令生成的pipe并没有加上这个decorator)
import { Injectable, Pipe } from '@angular/core';
@Pipe({
name: 'myPipe'
})
@Injectable()
export class MyPipe {
transform(value, args) {
//do something to 'value'
return value;
}
}
使用: {{ someMinding | myPipe:{arg1: true, arg2: false} }}
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class Data {
data: any;
constructor(public http: Http) {
console.log('Hello Data Provider');
}
load() {
if (this.data) {
return Promise.resolve(this.data);
}
// don't have the data yet
return new Promise(resolve => {
// We're using Angular Http provider to request the data,
// then on the response it'll map the JSON data to a parsed JS object.
// Next we process the data and resolve the promise with the new data.
this.http.get('path/to/data.json')
.map(res => res.json())
.subscribe(data => {
// we've got back the raw data, now generate the core schedule data
// and save the data for later reference
this.data = data;
resolve(this.data);
});
});
}
}
这样声明: providers: [Data]
这样使用:
import { Component } from '@angular/core';
import { Data } from '../../providers/data';
@Component({
selector: 'home-page',
templateUrl: 'home.html'
})
export class MyPage {
constructor(public dataService: Data){
}
method(){
this.dataService.load().then((data) => {
console.log(data);
});
}
<ion-header>
<ion-navbar color="secondary">
<ion-title>
My Friends
</ion-title>
<ion-buttons end>
<button ion-button icon-only (click)="doSomethingCool()"><ion-icon name="add-circle"></ion-icon></button>
</ion-buttons>
</ion-navbar>
</ion-header>
<ion-content>
<ion-searchbar (input)="getItems($event)"></ion-searchbar>
<ion-list>
<ion-item *ngFor="let item of items">
<ion-avatar item-left>
<img [src]="item.picture">
</ion-avatar>
<h2>{{item.name}}</h2>
<p>{{item.description}}</p>
</ion-item>
</ion-list>
</ion-content>
安装
更多
在跑
ic platform add ios
时出错:解决: 重装cordova | 升级xcode到最新版 |
rm -rf ~/.cordova
, 参考: 1 | 重新建项目跑
ic run ios --device
的时候报如下错误 Signing for "MyApp" requires a development team. Select a development team in the project editor. Code signing is required for product type 'Application' in SDK 'iOS 11.1'解决: (把config.xml中的id改成唯一的ID), 然后重新执行, 见 2,
open platforms/ios/MyApp.xcodeproj
然后把bundle id改一下重选一下team, 在xcode中跑一次..报错:
解决:
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
我的环境