首页 > 解决方案 > 可以在浏览器平台上使用带有 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 |     })

标签: reactjssqliteionic-frameworkpluginscordova-plugins

解决方案


推荐阅读