基于angular6实现数据变化监听的service
安装npm install ngx-watcher --save配置对于angular6项目不需要配置,Ok!
对于angular4,angular5配置app.module.ts文件
@NgModule({ ... providers: [NgxWatcherService], ...})export class AppModule { }使用 import {KvWatcher, IterWatcher, NgxWatcherService} from 'ngx-watcher'; @Component({...}) export class TestComponent implements DoCheck { private kvWatcher: KvWatcher<any>; private iterWatcher: IterWatcher<any>; value = {}; // keyValue类型 array = []; // 可迭代类型 constructor(private service: NgxWatcherService) { this.kvWatcher = service.of(this.value); this.iterWatcher = service.ofIter(this.array); } ngDoCheck(): void { this.kvWatcher.watch( this.value, v => console.log(`change after value:`, v), (t, v) => console.log(`changed value: WatchChangeType=${t} ${v.key} ${v.previousValue} ${v.currentValue}`) ); this.iterWatcher.watch( this.array, v => console.log(`change after value:`, v), (t, v) => console.log(`changed value: WatchChangeType=${t} ${v.currentIndex} ${v.item} ${v.previousIndex} ${v.trackById}`) ); } }关于本组件项目本项目使用AngularCLI version6.0.7生成
运行项目使用 ngserve启动开发服务.然后打开浏览器输入地址https://localhost:4200/ 即可
编译组件npm run build:lib生成编译后的文件在dist/ngx-watcher目录
运行效果LICENSEApache-2.0
评论