我试图向LokiJS添加监听器,以便在任何集合中的数据发生变化时通知我(尽管根据文档LokiJS支持数据更改通知)。但是,我无法收到有关数据更改的通知。
以下是我使用的代码段:
import { Component, OnInit } from '@angular/core';
import { Message } from 'primeng/components/common/api';
import { MessageService } from 'primeng/components/common/messageservice';
import * as loki from 'lokijs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
db: loki;
constructor(private messageService: MessageService) {
}
ngOnInit() {
this.db = new loki('kp-loki.db');
this.db.addCollection('kp_users', { asyncListeners: true });
console.log(this.db.addListener('changes', event => console.log(event)));
}
add() {
this.messageService.add({ severity: 'sucess', summary: 'data added' });
const uu = this.db.getCollection('kp_users');
uu.insert({ id: 1, name: 'kp', city: 'blr' });
}
view() {
const uu = this.db.getCollection('kp_users');
this.messageService.add({ severity: 'sucess', summary: JSON.stringify(uu.data) });
}
}
答案 0 :(得分:1)
好的,我会寻找这个轰鸣声事件
this.events = {
'init': [],
'loaded': [],
'flushChanges': [],
'close': [],
'changes': [],
'warning': []
};
但我应该使用以下事件,在更改侦听器事件时,它可以工作
// events
this.events = {
'insert': [],
'update': [],
'pre-insert': [],
'pre-update': [],
'close': [],
'flushbuffer': [],
'error': [],
'delete': [],
'warning': []
};