我正在尝试使用命令npm run build:ssr
为我的角度应用程序构建。成功地构建了应用程序,但是在运行commandnpm run serve:ssr
时,我得到了这个错误-
ReferenceError: IDBIndex is not defined
PS:根据我的发现,我使用的是Firebase软件包。我把全部内容都包在支票下,但还是有错误。
可能是这条进口线
import { openDb, deleteDb } from 'idb';
正在引发问题,有人能帮忙吗。
是否需要动态导入
if (isPlatformBrowser(this.platform)) {}
?PPS:我已经检查过答案,但不适合我
最佳答案
我不确定这对你有帮助,但我想试试
import { Component, OnInit } from '@angular/core';
import { openDb } from 'idb';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
public user: IUser;
private dbInstance;
/**
* idb.open(name, version, upgradeCallback)
* To ensure database integrity, object stores can only be created and removed in the callback function in idb.open.
* The callback receives an instance of UpgradeDB, a special object in the IDB Promised library that is used to create object stores
*/
ngOnInit(): void {
if (!('indexedDB' in window)) {
console.log('This browser doesn\'t support IndexedDB');
return;
}
this.dbInstance = openDb('demo-indexdb', 1, (upgradeDb) => {
console.log('making a new object store');
if (!upgradeDb.objectStoreNames.contains('people')) {
upgradeDb.createObjectStore('people', {keyPath: 'id', autoIncrement: true});
}
});
}
saveData() {
this.dbInstance.then((db) => {
const tx = db.transaction('people', 'readwrite');
const store = tx.objectStore('people');
const user: IUser = {
name: 'Admin',
email: '[email protected]',
description: 'admin of HR department',
created: new Date()
};
store.add(user);
return tx.complete;
}).then(() => {
console.log('added item to the store');
});
}
async getData() {
const db = await this.dbInstance;
return db.transaction('people').objectStore('people').get(1).then(val => this.user = val);
}
async updateData(key: any) {
const db = await this.dbInstance;
const tx = db.transaction('people', 'readwrite');
const user = {
id: key,
name: 'Admin1',
email: '[email protected]',
description: 'admin1 of HR department',
created: new Date().getTime()
};
tx.objectStore('people').put(user);
return tx.complete;
}
getAll() {
this.dbInstance.then((db) => {
const tx = db.transaction('people', 'readonly');
const store = tx.objectStore('people');
return store.getAll();
}).then((items) => {
console.log('Items by name:', items);
});
}
async delete(key: any) {
const db = await this.dbInstance;
const tx = db.transaction('people', 'readwrite');
tx.objectStore('people').delete(key);
return tx.complete;
}
}
关于angular - ReferenceError:未定义IDBIndex Angular SSR,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56536867/