标签:
今天,我们要讲的是angualr2的pipe这个知识点,但是在这之前我们需要升级一下我们的装备,因为之前的装备太“寒酸”了。
这个例子包含两个pipe,一个是stateful,一个是stateless,是直接复制官方的例子。本例子还包含了我对AngularClass/angular2-webpack-starter这个牛逼starter的改写,我会详细讲解配置。
这里面包含了等多方面的内容,堪称殿堂级的开发体验,快擦擦你的口水。
但是考虑到国内很多同学没有FQ,很多package装不了,我就删除了里面所有测试相关的npm依赖以及配置文件,如此一来,我可以保证每个同学都能顺利安装运行这个程序。另外,我们现在以学习为主,先不写测试代码。
但是值得一提的是,写测试是个很好的习惯,能很好的减少你的程序的bug率。
在你安装完整个依赖后,会安装typings的依赖。这个typings是用来干嘛的?我们为什么要用typings安装一些依赖?先看看这篇文章吧!
在 TypeScript 中使用 JavaScript 类库
简单来说,我们要在typescript中用一些js类库,必须要用*.d.ts 声明文件, 为原有 JS 文件和现有 TS 项目搭桥,而typings和tsd都是来安装这些声明文件的!否则你的程序会报错!说什么什么没定义!
这是官网,简单来说这是用来给你的ts程序生成文档的!你可以使用命令行来生成:npm run doc。
用于检查ts中的错误,俗称“去毛机”,可以让你养成良好的书写ts代码的习惯,比如类型和变量之间必须有空格:value: number。
这里我主要讲接口,因为webpack的插件太多了,用法也太多了,我们就直接复制拿来用,想了解具体配置的可以参考官网:
以上四个地方都是不需要更改的!直接拿来用!如果你心够大,甚至可以不搞懂里面配置,直接拿来用!但是我还是希望我们知其然知其所以然!
那么我们在哪里编写程序呢?在src/app中。这个文件夹中,除了app.ts只能改不能删之外,其他任何文件和文件夹都可以删除!
运行方法:
开发环境:npm start
生产环境:npm run build:prod npm run server:prod
装备升级完了,我们开始玩pipe。pipe就是ng1中的filter。先看看内建pipe吧:
currencydateuppercasejsonlimitTolowercaseasyncdecimalpercent无需编写直接用!今天说了太多“直接用”哈哈!
pipe分为两种,一种是stateful,一种是stateless。我们先说stateless,stateless就是使用纯函数,不记住任何数据,也不会带来任何副作用。DatePipe就是stateless,我们再写个计算次方的pipe吧:
src/app/stateless/exponential-strength.pipe.ts
import {Pipe, PipeTransform} from ‘angular2/core‘;
/*
* Raise the value exponentially
* Takes an exponent argument that defaults to 1.
* Usage:
* value | exponentialStrength:exponent
* Example:
* {{ 2 | exponentialStrength:10}}
* formats to: 1024
*/
@Pipe({name: ‘exponentialStrength‘})
export class ExponentialStrengthPipe implements PipeTransform {
transform(value: number, args: string[]) : any {
return Math.pow(value, parseInt(args[0] || ‘1‘, 10));
}
}
很简单,计算某个值的某次方,{{ 2 | exponentialStrength:10}}就是2的10次方。
写个模板测试下:
src/app/stateless/power-booster.component.ts
import {Component} from ‘angular2/core‘;
import {ExponentialStrengthPipe} from ‘./exponential-strength.pipe‘;
@Component({
selector: ‘power-booster‘,
template: `
<h2>Power Booster</h2>
<p>
Super power boost: {{2 | exponentialStrength: 10}}
</p>
`,
pipes: [ExponentialStrengthPipe]
})
export class PowerBooster { }
注入pipes: [ExponentialStrengthPipe],然后直接用!
先看一个stateful pipe的例子吧:
src/app/stateful/fetch-json.pipe.ts
declare var fetch; import {Pipe, PipeTransform} from ‘angular2/core‘; @Pipe({ name: ‘fetch‘, pure: false }) export class FetchJsonPipe implements PipeTransform { private fetchedValue: any; private fetchPromise: Promise<any>; transform(value: string, args: string[]): any { if (!this.fetchPromise) { this.fetchPromise = fetch(value) .then((result: any) => result.json()) .then((json: any) => this.fetchedValue = json); } return this.fetchedValue; } }
我们干了这些事:
写个模板测试下:
src/app/stateful/hero-list.component.ts
import {Component} from ‘angular2/core‘;
import {FetchJsonPipe} from ‘./fetch-json.pipe‘;
@Component({
selector: ‘hero-list‘,
template: `
<h4>Heroes from JSON File</h4>
<div *ngFor="#hero of (‘/assets/heroes.json‘ | fetch) ">
{{hero.name}}
</div>
<p>Heroes as JSON:
{{‘/assets/heroes.json‘ | fetch | json}}
</p>
`,
pipes: [FetchJsonPipe]
})
export class HeroListComponent {
/* I‘ve got nothing to do ;-) */
}
‘/assets/heroes.json‘是我自己编写的json文件,放在了assets里面,因为这是webpack的静态文件地址。
结果:
Stateful pipes are conceptually similar to classes in object-oriented programming. They can manage the data they transform. A pipe that creates an HTTP request, stores the response and displays the output, is a stateful pipe.
这是官方对statefule pipe的描述。说是能够创建http请求,存储响应,显示输出的pipe就是stateful pipe。那么stateless pipe不能做这些事吗?我好奇的在stateless pipe中尝试做http请求:
declare var fetch; import {Pipe, PipeTransform} from ‘angular2/core‘; @Pipe({ name: ‘fetch‘ }) export class FetchJsonPipe implements PipeTransform { transform(value: string, args: string[]): any { fetch(value) .then((result: any) => result.json()) .then((json: any) => json); } }
结果什么都输出不了!说明当我们需要使用http的时候,或者处理异步的时候,需要使用stateful pipe。
如果您觉得本博客教程帮到了您,就赏颗星吧!
https://github.com/lewis617/angular2-tutorial
标签:
原文地址:http://www.cnblogs.com/lewis617/p/5216381.html