nuxt-community / localforage-module

Localforage module for Nuxt.js
MIT License
88 stars 5 forks source link

Uncaught (in promise) TypeError: this.$localForage is undefined #15

Open metrey opened 3 years ago

metrey commented 3 years ago

The installation is following as:

npm install --save-dev @nuxtjs/localforage

Add @nuxtjs/localforage to the buildModules section of nuxt.config.js

buildModules: [
    // https://go.nuxtjs.dev/eslint
    // '@nuxtjs/eslint-module',
    // https://go.nuxtjs.dev/vuetify
    '@nuxtjs/vuetify',
    '@nuxtjs/localforage'
  ],

When use in a Vue component, still got this error: Uncaught (in promise) TypeError: this.$localForage is undefined

Also try to add in block of modules in nuxt.config.js also still error, this.$localForage not delcare by the module

metrey commented 3 years ago

So, now I fixed it by write a plugin follow the v-localforage: https://github.com/datalogix/v-localforage/blob/master/src/index.js

as following:

npm install localforage

Write plugin: plugin/localforage.client.js and load it as normal nuxtjs case:

import Vue from 'vue
import localforage from 'localforage'
Vue.use(VueLocalforage)
/* istanbul ignore next */
if (typeof window !== 'undefined' && window.Vue) {
  window.Vue.use(VueLocalforage)
}

// Place your option here
const options = {}

Vue.$localforage = localforage.createInstance(options)
Vue.prototype.$localforage = Vue.$localforage

if (options.instances) {
    for (const instance of options.instances) {
        const name = instance.storeName || instance.name
        Vue.$localforage[name] = localforage.createInstance(instance)
        Vue.prototype.$localforage[name] = Vue.$localforage[name]
    }
}

So for this module, need to fix as above solution, I don't need any module.

ricardogobbosouza commented 3 years ago

Hi @metrey I can't reproduce your problem

metrey commented 3 years ago

@ricardogobbosouza I just setup as mention in https://github.com/nuxt-community/localforage-module/issues/15#issue-839503403 and got that error, I don't know if at your side, you have some where that could load that.

As now, it works by using the solution I mention in https://github.com/nuxt-community/localforage-module/issues/15#issuecomment-806419070

ricardogobbosouza commented 3 years ago

Hi @metrey Please provide a repository for reproduction

sundayz commented 2 years ago

Happens to me too with a fresh default install of nuxt 2.15.7, Javascript with Babel (no typescript).