2017-04-17 36 views
10

阅读所有我能找到的,和失败后,我必须在这里问:Ionic2错误:“没有供应商的存储”

我想使用ionic2的存储,就像文档告诉我,

DOC:https://ionicframework.com/docs/storage/

这里是我的代码:

APP-module.ts

import { BrowserModule } from '@angular/platform-browser'; 
    import { ErrorHandler, NgModule } from '@angular/core'; 
    import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; 
    import { SplashScreen } from '@ionic-native/splash-screen'; 
    import { StatusBar } from '@ionic-native/status-bar'; 

    import { MyApp } from './app.component'; 
    import { HomePage } from '../pages/home/home'; 
    import { Intro } from '../pages/intro/intro'; 
    import { Checklist } from '../pages/checklist/checklist'; 
    // import { Http } from '@angular/http'; 
    import {IonicStorageModule} from '@ionic/Storage'; 
    import { Data } from '../providers/data'; 
    import {HttpModule} from '@angular/http'; 
    // import {Storage} from '@ionic/storage'; 


    @NgModule({ 
     declarations: [ 
     MyApp, 
     HomePage, 
     Intro, 
     Checklist 
     ], 
     imports: [ 
     HttpModule, 
     BrowserModule, 
     IonicModule.forRoot(MyApp), 
     IonicStorageModule.forRoot() 
     ], 
     bootstrap: [IonicApp], 
     entryComponents: [ 
     MyApp, 
     HomePage, 
     Intro, 
     Checklist 
     ], 
     providers: [ 
     StatusBar, 
     SplashScreen, 
     {provide: ErrorHandler, useClass: IonicErrorHandler}, 
     // Storage, 
     //Http, 
     Data 
     ], 
    }) 
    export class AppModule {} 


data.ts 

import { Injectable } from '@angular/core'; 
// import { Http } from '@angular/http'; 
import 'rxjs/add/operator/map'; 
// import { HttpModule } from '@angular/http'; 

import { Storage } from '@ionic/storage'; 


@Injectable() 
export class Data { 
    constructor(public storage: Storage) { 
    } 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    getData(): Promise<any> { 
    return this.storage.get('checklists'); 
    } 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    save(data): void { 
    let saveData = []; 
    //Remove observables 
    data.forEach((checklist) => { 
     saveData.push({ 
     title: checklist.title, 
     items: checklist.items 
     }); 
    }); 
    let newData = JSON.stringify(saveData); 
    this.storage.set('checklists', newData); 
    } 
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
} 

home.ts

// import { Component } from '@angular/core'; 
// import { NavController } from 'ionic-angular'; 

// @Component({ 
// selector: 'page-home', 
// templateUrl: 'home.html' 
// }) 
// export class HomePage { 

// constructor(public navCtrl: NavController) { 

// } 

// } 


import { Component } from '@angular/core'; 
import { NavController, AlertController, Platform } from 'ionic-angular'; 
import { Checklist } from '../checklist/checklist'; 
import { ChecklistModel } from '../../models/checklist-model'; 
import { Data } from '../../providers/data'; 
import { Keyboard } from 'ionic-native'; 
@Component({ 
    selector: 'page-home', 
    templateUrl: 'home.html', 
}) 
export class HomePage { 
    checklists: ChecklistModel[] = []; 

    constructor(public navCtrl: NavController, public dataService: Data, 
    public alertCtrl: AlertController, public platform: Platform) { 
    } 

    // constructor(public navCtrl: NavController, public alertCtrl: AlertController, public platform: Platform) { 
    // // this.checklists.push(new ChecklistModel("Noam", [1,2,3])); 
    // } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    ionViewDidLoad() { 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    addChecklist(): void { 
    let prompt = this.alertCtrl.create({ 
     title: 'New Checklist', 
     message: 'Enter the name of your new checklist below:', 
     inputs: [ 
     { 
      name: 'name' 
     } 
     ], 
     buttons: [ 
     { 
      text: 'Cancel' 
     }, 
     { 
      text: 'Save', 
      handler: data => { 
      let newChecklist = new ChecklistModel(data.name, []); 
      this.checklists.push(newChecklist); 
      newChecklist.checklistUpdates().subscribe(update => { 
       this.save(); 
      }); 
      this.save(); 
      } 
     } 
     ] 
    }); 
    prompt.present(); 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    renameChecklist(checklist): void { 
    let prompt = this.alertCtrl.create({ 
     title: 'Rename Checklist', 

     message: 'Enter the new name of this checklist below:', 
     inputs: [ 
     { 
      name: 'name' 
     } 
     ], 
     buttons: [ 
     { 
      text: 'Cancel' 
     }, 
     { 
      text: 'Save', 
      handler: data => { 
      let index = this.checklists.indexOf(checklist); 
      if (index > -1) { 
       this.checklists[index].setTitle(data.name); 
       this.save(); 
      } 
      } 
     } 
     ] 
    }); 
    prompt.present(); 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    viewChecklist(checklist): void { 
    this.navCtrl.push(Checklist, { 
     checklist: checklist 
    }); 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    removeChecklist(checklist): void { 
    let index = this.checklists.indexOf(checklist); 
    if (index > -1) { 
     this.checklists.splice(index, 1); 
     this.save(); 
    } 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
    save(): void { 
    Keyboard.close(); 
    this.dataService.save(this.checklists); 
    } 
    /////////////////////////////////////////////////////////////////////////////////////////////////////////// 
} 

是应该被调用,使用的储存方法是首页的save()

我不能走到这一步,然而,因为页面加载,甚至之前,我得到

Runtime Error Uncaught (in promise): Error: No provider for Storage! Error at g (http://localhost:8100/build/polyfills.js:3:7133) at injectionError (http://localhost:8100/build/main.js:1585:86) at noProviderError (http://localhost:8100/build/main.js:1623:12) at ReflectiveInjector_.throwOrNull (http://localhost:8100/build/main.js:3125:19) at ReflectiveInjector.getByKeyDefault (http://localhost:8100/build/main.js:3164:25) at ReflectiveInjector.getByKey (http://localhost:8100/build/main.js:3096:25) at ReflectiveInjector.get (http://localhost:8100/build/main.js:2965:21) at AppModuleInjector.get (ng:///AppModule/module.ngfactory.js:254:82) at AppModuleInjector.getInternal (ng:///AppModule/module.ngfactory.js:481:44) at AppModuleInjector.NgModuleInjector.get (http://localhost:8100/build/main.js:3929:44) at resolveDep (http://localhost:8100/build/main.js:11334:45) at createClass (http://localhost:8100/build/main.js:11202:32) at createDirectiveInstance (http://localhost:8100/build/main.js:11028:37) at createViewNodes (http://localhost:8100/build/main.js:12377:49) at createRootView (http://localhost:8100/build/main.js:12282:5)

的package.json:

{ 
    "name": "ionic-hello-world", 
    "author": "Ionic Framework", 
    "homepage": "http://ionicframework.com/", 
    "private": true, 
    "config": { 
    "ionic_source_map": "source-map" 
    }, 
    "scripts": { 
    "clean": "ionic-app-scripts clean", 
    "build": "ionic-app-scripts build", 
    "ionic:build": "ionic-app-scripts build", 
    "ionic:serve": "ionic-app-scripts serve" 
    }, 
    "dependencies": { 
    "@angular/common": "4.0.0", 
    "@angular/compiler": "4.0.0", 
    "@angular/compiler-cli": "4.0.0", 
    "@angular/core": "4.0.0", 
    "@angular/forms": "4.0.0", 
    "@angular/http": "4.0.0", 
    "@angular/platform-browser": "4.0.0", 
    "@angular/platform-browser-dynamic": "4.0.0", 
    "@ionic-native/core": "3.4.2", 
    "@ionic-native/splash-screen": "3.4.2", 
    "@ionic-native/status-bar": "3.4.2", 
    "@ionic/storage": "^2.0.1", 
    "ionic-angular": "3.0.1", 
    "ionic-native": "^2.9.0", 
    "ionicons": "3.0.0", 
    "rxjs": "5.1.1", 
    "sw-toolbox": "3.4.0", 
    "zone.js": "^0.8.4" 
    }, 
    "devDependencies": { 
    "@ionic/app-scripts": "1.3.0", 
    "typescript": "~2.2.1", 
    "webpack": "^2.4.1" 
    }, 
    "cordovaPlugins": [ 
    "cordova-plugin-whitelist", 
    "cordova-plugin-console", 
    "cordova-plugin-statusbar", 
    "cordova-plugin-device", 
    "cordova-plugin-splashscreen", 
    "ionic-plugin-keyboard" 
    ], 
    "cordovaPlatforms": [], 
    "description": "quicklists: An Ionic project" 
} 

因为我做的一切的文档说,请启迪我 - 什么仍然缺少,会导致存储不被发现

谢谢

+0

在您的应用程序模块中,您是否尝试将导入语句中的IonicStorageModule(forRoot)移动到提供程序数组中?似乎这个错误很难指向那个方向。不知道你是否需要forRoot部分,但我会尝试将它移出imports数组和提供者数组。很确定这是需要注射东西的地方。 – chairmanmow

+0

@chairmanmow http://stackoverflow.com/questions/42788623/local-storage-in-ionic-2-error在这里它告诉我做我做的,也是该文档https://ionicframework.com/docs/storage /状态它属于进口 – Gulzar

+1

嗯,是的,我猜你遵循指示。我注意到可能涉及到的一件事,我注意到您正在从'@ ionic/storage'导入'import {Storage};'一个地方,并从'@ ionic/Storage'导入导入{Storage}; ...注意S在其中的一个大写。无论如何,不​​知道我有多少帮助,但也许这是一个错字。 – chairmanmow

回答

12

首先,你需要安装:npm install --save @ionic/storage

问题是

在应用程序中。TS

import {IonicStorageModule} from '@ionic/Storage'; 

资本 'S' 而不是非资本的'

istead的

from '@ionic/storage' 

不知道

from '@ionic/Storage' 

为什么不编译器捕捉,如果这是一个问题,但事实并非如此。

由于@chairmanmow

+0

https://github.com/ionic-team/ionic-storage/releases/tag/v2.0.0运行npm install @ ionic/storage @ 2.0.0 --save --save-exact 从您的移除存储提供商在app.module.ts 从'@ ionic/storage'导入{IonicStorageModule},而不是从app.module.ts中的'@ ionic/storage'导入{IonicStorage} 将IonicStorageModule.forRoot()添加到imports数组中app.module.ts – JGFMK

5

先做这个NPM安装--save @离子/存储

我设法得到这个工作使用这个..

app.module.ts

import { Storage } from '@ionic/storage'; 

然后......

providers: [{provide: ErrorHandler, useClass: IonicErrorHandler}, Storage] 

然后在我的page.ts

import { Storage } from '@ionic/storage'; 

在构造...

public storage: Storage 

内,然后我的代码的胆量..

this.storage.get('date').then((value) => { 
    // blah 
}); 
2
Please use this plugin for native storage   

ionic cordova plugin add cordova-plugin-nativestorage 
npm install --save @ionic-native/native-storage 

和进口app.module.ts

import { NativeStorage } from '@ionic-native/native-storage'; 

    providers: [ 
    StatusBar, 
    SplashScreen, 
    NativeStorage, 
     LocalStorageProvider, 
    {provide: ErrorHandler, useClass: IonicErrorHandler} 

    ] 

进入这里

+0

它适用于我 –

4

代码就我而言,我忘了添加在app.module以下.ts

import { IonicStorageModule } from '@ionic/storage';
@NgModul({ ..., Imports: [ ... IonicStorageModule.forRoot() ],