菜鸟教程小白 发表于 2022-12-11 18:25:07

android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误


                                            <p><blockquote>
<p>I&#39;m trying to implement this simple example given in ionic 2 doc:
<a href="http://ionicframework.com/docs/v2/native/sqlite/" rel="noreferrer noopener nofollow">http://ionicframework.com/docs/v2/native/sqlite/</a></p>
</blockquote>

<p>我在 <code>MAC</code> 上尝试了该示例(对放置在 ionic 项目的 <code>'www\test.sqlite'</code> 文件夹下的数据库执行查询),我都收到此错误在浏览器和 iOS 模拟器上(也不适用于设备):</p>

<p><strong>ReferenceError: sqlitePlugin 未定义</strong></p>

<p>我已将 cordova-sqlite-storage 插件添加到 ionic 项目中。</p>

<p>代码:</p>

<pre><code>constructor(public navCtrl: NavController, public platform: Platform,
                public pps: ProdPerfService){
      platform.ready().then((readySource) =&gt; {
            pps.getSummary(); //pps is a provider named ProdPerfService
      });
    }

//ProdPerfService:
import { Injectable } from &#39;@angular/core&#39;;
import { SQLite } from &#39;ionic-native&#39;;

@Injectable()
export class ProdPerfService {
    constructor(){

    }

    getSummary(){
      let db = new SQLite();
      db.openDatabase({
            name: &#39;test.sqlite&#39;,
            location: &#39;default&#39; // the location field is required
      }).then(() =&gt; {
            db.executeSql(&#39;select * from summary&#39;, {}).then(() =&gt; {
                alert(&#39;result&#39;);

      }, (err) =&gt; {
            console.error(&#39;Unable to execute sql: &#39;, err);
            alert(&#39;err&#39;);
      })
      }, (err) =&gt; {
            console.error(&#39;Unable to open database: &#39;, err);
            alert(err);
      });
    }

}
</code></pre>

<blockquote>
<p>ionic details: Cordova CLI: 6.4.0 Ionic Framework Version: 2.0.0-rc.3
Ionic CLI Version: 2.1.17 Ionic App Lib Version: 2.1.7 Ionic App
Scripts Version: 0.0.45 ios-deploy version: Not installed ios-sim
version: Not installed OS: OS X El Capitan Node Version: v7.2.1 Xcode
version: Xcode 8.1 Build version 8B62</p>
</blockquote></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>尝试将你的逻辑移出构造函数。</p>

<pre><code>ionViewDidLoad(){
   this.platform.ready().then((readySource) =&gt; {
       this.pps.getSummary(); //pps is a provider named ProdPerfService
   });
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/41330841/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/41330841/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: android - 在 ionic 2 中使用 SQLite 时出现 sqlitePlugin 错误