以下概念介绍,基于 node v10.16.3 版本进行。不满足条件的,请移步 google or 度娘。
const a = 1,b = 2;
const c = a b;
响应式编程,当a,b再次变化时,c会再次做出修改。
npm install --global rollup
npm install rxjs
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
of(1,2,3).subscribe(res => {
console.log(res);
});
rollup打包→ bundle.js → node bundle.js,连续输出123
rollup main.js --file bundle.js --format cjs
'use strict';
var rxjs = require('rxjs');
require('rxjs/operators');
rxjs.of(1,2,3).subscribe(res => {
console.log(res);
});
创建rollup.config.js
export default {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'cjs'
}
};
rollup -c rollup.config.js
package.json
{
"name": "rxjs-demo",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build":"rollup -c", // 默认配置 rollup --config rollup.config.js
"start":"npm run build && node bundle.js" // && 是继发,& 是并发
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"rxjs": "^6.5.3"
}
}
最终的运行如下:
npm start
// install plugin
npm install --save-dev rollup-plugin-json
// rollup.config.js
import json from 'rollup-plugin-json';
export default {
input: 'main.js',
output: {
file: 'bundle.js',
format: 'cjs'
},
plugins:[json()]
};
// main.js
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
of(1,2,3,version).subscribe(res => {
console.log(res);
});
import { of } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
of(1,2,3,version).pipe(map(item => item ' through pipe use map'))
.subscribe(res => {
console.log(res);
});
其中,可以看到observable属于异步执行。
import { of,Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
//through pipe
// of(1,2,3,version).pipe(map(item => item ' through pipe use map'))
// .subscribe(res => {
// console.log(res);
// });
console.log("before observable....");
const publisher$ = new Observable(suber => {
console.log("in observable....");
suber.next(1);
suber.next(2);
suber.next(3);
suber.next(version);
});
console.log("after observable....");
publisher$.pipe(map(item => item ' through pipe use map')).subscribe(res => {
console.log(res);
});
console.log("before observable....");
const publisher$ = new Observable(suber => {
console.log("in observable....");
suber.next(1);
suber.next(2);
suber.next(3);
suber.next(version);
});
console.log("after observable....");
publisher$.pipe(map(item => item ' through pipe use map')).subscribe(res => {
console.log(res);
});
publisher$.subscribe(res => {
console.log("subscribe second..." res);
});
import { of,Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
let count = 0;
console.log("before observable....");
const publisher$ = new Observable(suber => {
console.log("in observable....");
setInterval(() => {
count ;
suber.next(count);
}, 100);
});
console.log("after observable....");
publisher$.pipe(map(item => 'first ...' item)).subscribe(res => {
console.log(res);
});
setTimeout(() => {
const publisher2$ = new Observable(suber => {
setInterval(() => {
suber.next(count);
}, 100);
});
publisher2$.subscribe(res => {
console.log("second ..." res);
});
}, 1000);
import { of,Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
console.log("before observable....");
const publisher$ = new Observable(suber => {
console.log("in observable....");
setInterval(() => {
let count = 0;
count ;
suber.next(count);
}, 100);
});
console.log("after observable....");
publisher$.pipe(map(item => 'first ...' item)).subscribe(res => {
console.log(res);
});
setTimeout(() => {
const publisher2$ = new Observable(suber => {
setInterval(() => {
let count = 0;
count ;
suber.next(count);
}, 100);
});
publisher2$.subscribe(res => {
console.log("second ..." res);
});
}, 1000);
import { of,Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
const publiser$ = new Observable(suber => {
suber.next(1);
suber.next(2);
suber.next(3);
suber.next(version);
// suber.complete();
suber.error();
});
publiser$.subscribe({
next:res => {
console.log(`next ...${res}`);
},
complete:() => {
console.log("complete done....");
},
error: () => {
console.log("error occur....");
}
});
import { of,Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
const publiser$ = new Observable(suber => {
let count = 0;
const timer = setInterval(() => {
suber.next(count);
count ;
},400);
//suber.next(1);
// suber.complete();
// suber.error();
return {
unsubscribe:() => {
console.log("stop push data....");
clearInterval(timer);
}
}
});
const unsuber = publiser$.subscribe({
next:res => {
console.log(`next ...${res}`);
},
complete:() => {
console.log("complete done....");
},
error: () => {
console.log("error occur....");
}
});
setTimeout(() => {
unsuber.unsubscribe();
},2000);
import { of,Observable, interval } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
const publiser$ = interval(400);
const unsuber = publiser$.subscribe({
next:res => {
console.log(`next ...${res}`);
},
complete:() => {
console.log("complete done....");
},
error: () => {
console.log("error occur....");
}
});
setTimeout(() => {
unsuber.unsubscribe();
},2000);
import { of,Observable, interval,Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
const sub = new Subject();
sub.pipe(map(res => 'first ...' res)).subscribe((res) => {
console.log("sub one...",res);
});
sub.subscribe({
next:(res) => {
console.log(`second...${res}`);
}
});
sub.next(1);
sub.next(2);
import { of,Observable, interval,Subject ,from} from 'rxjs';
import { map } from 'rxjs/operators';
import { version } from './package.json';
const sub = new Subject();
sub.pipe(map(res => 'first ...' res)).subscribe((res) => {
console.log("sub one...",res);
});
sub.subscribe({
next:(res) => {
console.log(`second...${res}`);
}
});
// sub.next(1);
// sub.next(2);
const publisher$ = from([1,2,3]);
publisher$.subscribe(sub);
本文作者:前端首席体验师(CheongHu)
联系邮箱:[email protected]