reactjs - 可以在浏览器平台上使用带有 Ionic React 的 SQLite 插件吗?
问题描述
当它在浏览器中运行时,是否可以在我的 Ionic React WebApp 中使用 SQLite ( SQLite - Ionic Documentation )?SQLite 是一个 Cordova 插件,但它也可以在 React 应用程序中使用:React 应用程序使用 Capacitor 来构建原生移动应用程序。但是,可以使用 Ionic Native(以及 Cordova 插件)(React - Ionic Native Community - Ionic Documentation)。该插件的官方文档说现在支持浏览器平台(GitHub - storesafe/cordova-sqlite-storage: ...),但并没有具体提到 Ionic 或 React。
我不明白如何让 SQLite 插件在浏览器中与 React 一起工作?
此代码不会引发任何错误,根据我的阅读,它可以在 Android 或 iOS 手机上运行:
import { IonAlert, IonButton, IonContent, IonHeader, IonPage, IonTitle, IonToolbar } from '@ionic/react';
import React, { useState } from 'react';
import ExploreContainer from '../components/ExploreContainer';
import './Home.css';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite'
const Home: React.FC = () => {
const [showAlert, setShowAlert] = useState(false);
const initDb = (): void => {
SQLite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql('create table danceMoves(name VARCHAR(32))', [])
.then(() => console.log('Executed SQL'))
.catch(e => console.log(e));
})
.catch(e => console.log(e));
};
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Blank</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton onClick={initDb}>Init DB</IonButton>
</IonContent>
</IonPage>
);
};
export default Home;
在浏览器(离子服务)中,我收到此错误消息,然后是几十个黄色错误:
TypeError: can't access property "then", ionic_native_sqlite__WEBPACK_IMPORTED_MODULE_5 _.SQLite.create(...) is undefined initDb C:/Users/UserAccount/Documents/Ionic-Projekte/Test-1-SQLite-Blank/src/pages/主页.tsx:12
9 | const Home: React.FC = () => {
10 | const [showAlert, setShowAlert] = useState(false);
11 | const initDb = (): void => {
> 12 | SQLite.create({
13 | name: 'data.db',
14 | location: 'default'
15 | })
解决方案
推荐阅读
- c# - RedirectToAction、RedirectToRoute 和 RedirectToLocal 导致应用程序间歇性挂起
- ionic-framework - Ionic 4 本机设备插件在平台浏览器上返回设备 uuid null
- java - 是否可以在 JNLP 文件的 j2se 标签中指定多个 java 版本?
- c - 递归函数要迭代,这个函数给定g(n) = 2*g(n-1)+3*g(n-2)
- vb.net - 我可以为“值类型”函数启用“函数不会在所有代码路径上返回值”警告吗?
- angular - 如何删除 ngx 日期选择器中的自动更正?
- c# - 如何将 numpy .npz 文件直接读入 C#
- c - opengl不显示renderFunction的输出
- java - 使用 foreach 将 arrayList 与自身进行比较,不包括重复项
- airflow-scheduler - 气流 - 如何仅“填充 DagBag”一次