Angular 学习笔记 immer 使用
https://github.com/immerjs/immer#supported-object-types
immer 是用来做 immutable 的.
angular 的 change detech 机制, 有时候为了要性能好一些,我们需要用 onPush 然后得配合 immutable 来让 input 触发.
但是呢, immuable.js 写起来很丑, 原生 es6 得写法也不好看. 于是就有了 immer.
早前 immer 完全不支持 class 所以我就没有用,最近看了一下发现部分支持了. 所以开始用了.
import { produce, immerable } from "immer";
class Product {
constructor(data?: Partial<Product>) {
Object.assign(this, data);
}
[immerable] = true;
date: Date;
colors: Color[];
private _price : number;
public get price() : number {
return this._price;
}
public set price(v : number) {
this._price = v;
}
}
class Color {
constructor(data? : Partial<Color>) {
Object.assign(this, data);
}
[immerable] = true;
text: string
}
const product = new Product({ date: new Date(), price: 50, colors : [new Color({ text : 'dada' })] });
const newProduct = produce(product, next => {
next.price = 10;
});
console.log('instanceof Product', newProduct instanceof Product);
console.log('is new', newProduct !== product);
console.log('new value', newProduct.price === 10);
用法很简单. 调用, produce, 然后把对象传进去, next 是一个 proxy 对象, 我们像一般得操作方式就可以了, 最后会返回新得对象.
它并不是 clone 整个子孙对象哦,而是你有修改才会 clone.
如果是 class 要加上一个 symbol [immerable] = true
不支持的地方很多要特别注意哦 :
1. 如果要修改 Date 的, 必须自己 clone 一个新的.
2. array 只可以修改 length 不可以自己添加额外的属性.
3. prototype 是不会 clone 的, 保留原本指针
4. Built-in classes like Map and Set 不支持 (官网给了 workaround, 我也没用, 所以暂时不管)
5. 不支持 computed property (如果是一般写 getter setter 是可以的, 因为 getter setter 其实是 define 到了 prototype 上而不是对象上面, 如果你要 define 到对象上, 那 immer 就支持不到了)
Angular 学习笔记 immer 使用的更多相关文章
- angular学习笔记(三十一)-$location(2)
之前已经介绍了$location服务的基本用法:angular学习笔记(三十一)-$location(1). 这篇是上一篇的进阶,介绍$location的配置,兼容各版本浏览器,等. *注意,这里介绍 ...
- angular学习笔记(三十一)-$location(1)
本篇介绍angular中的$location服务的基本用法,下一篇介绍它的复杂的用法. $location服务的主要作用是用于获取当前url以及改变当前的url,并且存入历史记录. 一. 获取url的 ...
- angular学习笔记(三十)-指令(10)-require和controller
本篇介绍指令的最后两个属性,require和controller 当一个指令需要和父元素指令进行通信的时候,它们就会用到这两个属性,什么意思还是要看栗子: html: <outer‐direct ...
- angular学习笔记(三十)-指令(7)-compile和link(2)
继续上一篇:angular学习笔记(三十)-指令(7)-compile和link(1) 上一篇讲了compile函数的基本概念,接下来详细讲解compile和link的执行顺序. 看一段三个指令嵌套的 ...
- angular学习笔记(三十)-指令(7)-compile和link(1)
这篇主要讲解指令中的compile,以及它和link的微妙的关系. link函数在之前已经讲过了,而compile函数,它和link函数是不能共存的,如果定义了compile属性又定义link属性,那 ...
- angular学习笔记(三十)-指令(6)-transclude()方法(又称linker()方法)-模拟ng-repeat指令
在angular学习笔记(三十)-指令(4)-transclude文章的末尾提到了,如果在指令中需要反复使用被嵌套的那一坨,需要使用transclude()方法. 在angular学习笔记(三十)-指 ...
- angular学习笔记(三十)-指令(5)-link
这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...
- angular学习笔记(三十)-指令(2)-restrice,replace,template
本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...
- angular学习笔记(三十)-指令(1)-概述
之前在 angular学习笔记(十九)-指令修改dom 里面已经简单的提到了angular中的指令,现在来详细的介绍 '指令' 一.指令的创建: dirAppModule.directive('dir ...
随机推荐
- phpstorm 断点调试总是从index.php的第一行开始
去掉勾选,重开phpstorm
- 关于微信手机端IOS系统中input输入框无法输入的问题
如果网站不需要阻止用户的选择内容的行为就可以使用如下样式: * { -webkit-user-select: text; -user-select: text;}另一种方式: *: not(input ...
- 海思3531添加移远EC20 4g模块(转)
源: 海思3531添加移远EC20 4g模块 Hi3798移植4G模块(移远EC20)
- flutter 页面布局 Paddiing Row Column Expanded 组件
Flutter Paddiing 组件 在 html 中常见的布局标签都有 padding 属性,但是 Flutter 中很多 Widget 是没有 padding 属 性.这个时候我们可以用 Pad ...
- Vue动态路由 Get传值
<template> <!-- 所有的内容要被根节点包含起来 --> <div id="home"> 我是首页组件 <ul> < ...
- 获取并打印Spring容器中所有的Bean名称
思路: 1.实现Spring的ApplicationContextAware接口,重写setApplicationContext方法,将得到的ApplicationContext对象保存到一个静态变量 ...
- shell编程系列8--文本处理三剑客之grep和egrep
grep和egrep 第一种形式:grep [option] [pattern] [file1,file2...] 第二种形式:command | grep [option] [pattern] gr ...
- GANomaly: Semi-Supervised Anomaly Detection via Adversarial Training-1-论文学习
通过对抗训练实现半监督的异常检测 Abstract 异常检测在计算机视觉中是一个经典的问题,即从异常中确定正常,但是由于其他类(即异常类)的样本数量不足,所以数据集主要基于一个类(即正常类).虽然该问 ...
- 【Mybatis】MyBatis之缓存(七)
MyBatis缓存介绍 Mybatis 使用到了两种缓存:一级缓存(本地缓存.local cache)和二级缓存(second level cache). 一级缓存:基于PerpetualCache ...
- LeetCode_290. Word Pattern
290. Word Pattern Easy Given a pattern and a string str, find if str follows the same pattern. Here ...