简体   繁体   English

"带有 vue 的离子存储"

[英]Ionic-storage with vue

I'm building an application using Ionic Framework v5 with Vue 3.0.0 .我正在使用带有Vue 3.0.0Ionic Framework v5构建应用程序。

I use Vuex store to store global app informations, such as the user login token.我使用Vuex store来存储全局应用信息,例如用户登录令牌。

// file src\store.ts
export const store = createStore({
    state(){
      return { 
        loginToken;
      }
    },
    mutations: {
      updateLoginToken(state: any, token: string){
        state.loginToken = token;
      }
    },
    getters: {
      token(state: any){
        return state.loginToken;
      }
    }
})

I also need that this token is saved when the app is closed or refreshed, so i need to store it in some kind of local storage.我还需要在应用程序关闭或刷新时保存此令牌,因此我需要将其存储在某种本地存储中。

Official documentation suggest to use @ionic/storage to have access to a storage engines on multiple platforms.官方文档建议使用@ionic/storage来访问多个平台上的存储引擎。

To initialize the store i need to execute this code:要初始化商店,我需要执行以下代码:

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

const storage = new Storage();
await storage.create();

I'm pretty new to all this and i can't find where to place this code to initialize the storage, and also how can i then access to the storage object inside vuex store, to execute storage.set(...) and storage.get(...) ?我对这一切都很陌生,我找不到在哪里放置这段代码来初始化存储,以及如何访问 vuex 存储中的存储对象,执行storage.set(...)storage.get(...)

@ionic/storage vs @capacitor/storage @ionic/storage 与 @capacitor/storage

First you need to verify, if you really need @ionic/storage , or if @capacitor/storage is sufficient (in most cases it is).首先,您需要验证您是否真的需要@ionic/storage ,或者@capacitor/storage是否足够(在大多数情况下是这样)。 Using @ionic/storage requires an instance to be shared accross the app.使用@ionic/storage需要在整个应用程序中共享一个实例。 @capacitor/storage can be used directly via imports. @capacitor/storage可以通过导入直接使用。

A comparison between these plugins can be found here: Capacitor storage or cordova storage这些插件之间的比较可以在这里找到: 电容器存储或科尔多瓦存储

In this answer, I assume you really want to use @ionic/storage so you need to create an instance first, which we want to share.在这个答案中,我假设您真的想使用@ionic/storage所以您需要首先创建一个我们想要共享的实例。

Plugin to share @ionic/storage instance accross your app用于在您的应用程序中共享 @ionic/storage 实例的插件

Create a vue plugin, where you create the storage instance and save it in a global variable.创建一个 vue 插件,在其中创建存储实例并将其保存在全局变量中。 If needed also attach it to your VUEX store.如果需要,也可以将它附加到您的 VUEX 商店。

Somewhere in your main.js you attach the plugin to your Vue app:在你的 main.js 中的某个地方,你将插件附加到你的 Vue 应用程序:

app
  .use(...)
  .use(yourPlugin)

The plugin code:插件代码:

import { Storage } from '@ionic/storage'

install: async (app) => {
  const storage = new Storage()
  const storageInstance = await storage.create()

  app.config.globalProperties.$ionicStorage = storageInstance
  app.config.globalProperties.$store.$ionicStorage = storageInstance // This one is only needed if you want to access the ionic storage instance in your VUEX store actions
}

Write data to Device Storage将数据写入设备存储

In your case you want to write something to the Device Storage if a specific VUEX mutation occurs.在您的情况下,如果发生特定的 VUEX 突变,您想向设备存储写入一些内容。 There are multiple options to write to the device storage in this case.在这种情况下,有多个选项可以写入设备存储。

Either you run the mutation within a VUEX action like this and save it to the Device Storage afterwards:您可以像这样在 VUEX 操作中运行突变,然后将其保存到设备存储中:

actions: {
  async setLoginToken ({ commit, state }, loginToken) {
    commit('updateLoginToken', loginToken)
    this.$ionicStorage.set({
      key: 'your-storage-key',
      value: JSON.stringify({ loginToken })
    })
  }
}

This approach makes sense, if you only want to sync just a few VUEX store properties.如果您只想同步几个 VUEX 存储属性,这种方法是有意义的。 If you want to sync everything, or want to keep things more separated, you can attach a VUEX plugin to your VUEX store and subscribe to mutations.如果你想同步所有东西,或者想让事情更加分离,你可以将一个 VUEX 插件附加到你的 VUEX 商店并订阅突变。

How to attach VUEX plugins: https://vuex.vuejs.org/guide/plugins.html如何附加 VUEX 插件: https ://vuex.vuejs.org/guide/plugins.html

In general, registering a VUEX plugin to your store allows you to subscribe to mutations.通常,将 VUEX 插件注册到您的商店允许您订阅突变。 In your case the updateLoginToken mutation runs every time the login token gets updated.在您的情况下,每次更新登录令牌时都会运行updateLoginToken突变。 So the plugin needs to take care to write the new loginToken to the device storage after the mutation ran.所以插件需要注意在突变运行后将新的 loginToken 写入设备存储。

This will look similar to this:这看起来类似于:

const myPlugin = store => {
  // called when the store is initialized
  store.subscribe((mutation, state) => {
    // called after every mutation.
    // The mutation comes in the format of `{ type, payload }`.
    // Make sure the code runs only for the relevant mutation

    store.$ionicStorage.set({
      key: 'your-storage-key',
      value: JSON.stringify(xxx)
    })
  })
}

Read data from the Device Storage从设备存储中读取数据

Now you have the login token on the device storage.现在您在设备存储中拥有了登录令牌。 When the App restarts you need to grab it from there and push it to the VUEX store.当应用程序重新启动时,您需要从那里抓取它并将其推送到 VUEX 商店。 There are different approaches to set this up.有不同的方法来设置它。 I personally prefer to load it in a custom Vue plugin.我个人更喜欢将它加载到自定义的 Vue 插件中。

First you attach the plugin to your vue app again.首先,您再次将插件附加到您的 vue 应用程序。 Make sure this plugin is attached AFTER the plugin from above (The plugin which shares the ionicStorage instance globally).确保此插件附加在上面的插件之后(全局共享 ionicStorage 实例的插件)。

In the plugin you then just read the saved values from the device Storage and write it to your VUEX store.在插件中,您只需从设备存储中读取保存的值并将其写入您的 VUEX 存储。

export default {
  install: async (app) => {
    const storageContent = await app.config.globalProperties.$ionicStorage.get({ key: 'your-storage-key' })
    const storageContentParsed = JSON.parse(storageContent.value || '{}')
    const $store = app.config.globalProperties.$store

    $store.replaceState({
      ...$store.state,
      ...storageData
    })
  }
}

Note: These are just minimal code snippets.注意:这些只是最小的代码片段。 So make sure to adapt it to your own use case.因此,请确保使其适应您自己的用例。 In general this should make sure the loginToken gets synced to your device storage properly (on mobile and web).一般来说,这应该确保 loginToken 正确同步到您的设备存储(在移动设备和网络上)。

What i've done to make this all to work:我为使这一切正常工作所做的一切:

I've created a plugin file called Storage.ts that creates the plugin instance and adds it to app global properties:我创建了一个名为Storage.ts的插件文件,它创建了插件实例并将其添加到应用程序全局属性中:

import { Storage } from '@ionic/storage'

export default async function storageFactory(wsBaseUrl: string) {

  const storage = new Storage();
  // create storage
  const storageInstance = await storage.create();

  // my custom stuff here ...

  // in the end, return the plugin installer
  return {
    install(app: any){
      app.config.globalProperties.$ionicStorage = storageInstance;
      // special copy for vuex storage
      app.config.globalProperties.$store.$ionicStorage = storageInstance; 
    }
  }
}

Then in the main.ts i've added my custom plugin to the main app:然后在main.ts中,我将自定义插件添加到主应用程序中:

import storageFactory from '@/plugins/Storage';

// create storage plugin 
const storagePlugin = await storageFactory(config.wsBaseUrl);

// create app
const app = createApp(App)
  // ... other stuff here
  .use(storagePlugin)

// mount when router is ready
router.isReady().then(() => {
  app.mount('#app');
});

Then, anywhere in other app's files i use the storage like this:然后,在其他应用程序文件的任何地方,我都会像这样使用存储:

// set new key
await (this as any).$ionicStorage.set('token', token );
// remove the key
await (this as any).$ionicStorage.remove('token');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM