前端Rollup RxJs响应式编程实践

响应式编程

以下概念介绍,基于 node v10.16.3 版本进行。不满足条件的,请移步 google or 度娘。

概念介绍

const a = 1,b = 2;
const c = a  b;

响应式编程,当a,b再次变化时,c会再次做出修改。

背景
  • RxJS是一个用于使用Observables进行响应式编程的库
  • 可简化编写异步或基于回调的代码
本地环境搭建
  • node
  • npm script
  • rollup
 npm install --global rollup
  • rxjs
 npm install rxjs
  • es6 语法,利用tree-shaking ,缩减代码

使用

hello-world 之123
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配置

创建rollup.config.js

export default {
  input: 'main.js',
  output: {
    file: 'bundle.js',
    format: 'cjs'
  }
};
rollup -c rollup.config.js 
npm script支持

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值