当我致电storage.get(key)

时间:2019-01-02 18:41:38

标签: ionic-framework ionic-storage

我尝试在登录应用程序以及进入个人资料页面时尝试设置用户信息。

使用单独的服务设置并获取用户信息Im

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable, EventEmitter } from '@angular/core';
import {Storage} from '@ionic/storage';

@Injectable()
export class LoginService {

    private loginUrl = UrlService.APIURL+'/login/';
    userEvent: EventEmitter<any> = new EventEmitter();

    constructor(private http: HttpClient,
                private storage:Storage) { }


    public setStorageUser(user:any){

         return this.storage.set('user', user);

    }
    public getStorageUser():Promise<any>{

            return this.storage.get('user');

    }




}

在登录页面上设置值

this.loginService.setStorageUser(userData).then(()=>{

        this.navController.setRoot(HomePage);

        });

在“个人资料”页面上检索用户数据

this.loginService.getStorageUser().then((user)=>{
       console.log(JSON.stringify(user));
    });

我的应用程序上的应用程序模块页面

import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';
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 { AppAvailability } from '@ionic-native/app-availability';
import { InAppBrowser } from '@ionic-native/in-app-browser';
import { AndroidExoplayer } from '@ionic-native/android-exoplayer';
import { IonicStorageModule } from '@ionic/storage';
import {PayPal} from '@ionic-native/paypal';

import { FileTransfer } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';

import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { LoginPage } from '../pages/login/login';
import {ChannelListPage} from '../pages/channel-list/channel-list';
import {ListsPage} from '../pages/lists/lists';

import { LoginService } from '../providers/login.service';
import { UserService } from '../providers/user.service';
import { FileServiceProvider } from '../providers/file-service';
import { CategoryService } from '../providers/category.service';
import { ChannelService } from '../providers/channel.service';
import { SubscriptionService } from '../providers/subscription.service';

import {SideMenuPage} from '../pages/side-menu/side-menu';
import { CategoryPage} from '../pages/category/category';
import { ChannelsPage} from '../pages/channels/channels';
import { FavoritePage} from '../pages/favorite/favorite';
import { ChannelPage} from '../pages/channel/channel';
import { CategoryDetailPage} from '../pages/category-detail/category-detail';
import {PlayerPage} from '../pages/player/player';
import {RegisterPage} from '../pages/register/register';
import {PaymentPage} from '../pages/payment/payment';
import {ProfilePage} from '../pages/profile/profile';


@NgModule({
  declarations: [
    ChannelListPage,
    ListsPage,
    MyApp,
    HomePage,
    LoginPage,
    SideMenuPage,
    FavoritePage,
    CategoryPage,
    ChannelsPage,
    CategoryDetailPage,
    PlayerPage,
    ChannelPage,
    RegisterPage,
    PaymentPage,
    ProfilePage
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    IonicModule.forRoot(MyApp),
    IonicStorageModule.forRoot()

  ],
  bootstrap: [IonicApp],
  entryComponents: [
    ChannelListPage,
    ListsPage,
    MyApp,
    HomePage,
    LoginPage,
    SideMenuPage,
    CategoryPage,
    ChannelsPage,
    FavoritePage,
    CategoryDetailPage,
    PlayerPage,
    ChannelPage,
    RegisterPage,
    PaymentPage,
    ProfilePage
  ],
  providers: [

    File,
    FileTransfer,
    StatusBar,
    SplashScreen,
    AppAvailability,
    AndroidExoplayer,
    InAppBrowser,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    FileServiceProvider,
    LoginService,
    CategoryService,
    ChannelService,
    SubscriptionService,
    UserService,
     PayPal
  ]
})
export class AppModule {}

我总是得到空用户,帮助!

0 个答案:

没有答案