【发布时间】:2020-08-09 05:33:06
【问题描述】:
我在我的 SSR Nuxt 项目中使用localForage。
在我的Nuxt-Page-Component
...
<script>
import localforage from 'localforage';
export default{
mounted(){
localforage.getItem('something', value => {
...
});
}
}
</script>
...
因为localforage只能在浏览器中使用,所以每次我尝试以服务器端渲染模式访问页面时都会出现如下错误 (虽然页面可以被渲染并且页面按我想要的方式工作)
错误未找到可用的存储方法。在 node_modules/localforage/dist/localforage.js:2743:25
我尝试在我的项目中使用localforage 作为自定义插件,我将其配置为仅限客户端的插件
// nuxt.config.js
module.exports = {
...
plugins:[
{ src: '~/plugins/localforage', ssr: false }
],
...
}
// localforage.js
import localforage from 'localforage';
window.localforage = localforage;
// localforage.js (or as a Vue plugin)
import localforage from 'localforage';
import Vue from 'vue';
Vue.use({
install(Vue){
Vue.prototype.$localforage = localforage;
}
});
但我只能得到更多的错误,而且这一次页面无法渲染。
我该如何解决这个错误,我已经用谷歌搜索过了,但没有任何帮助。
非常感谢!
(不是英语母语,如有错误请见谅)
【问题讨论】:
-
为什么不在 if 语句中说明?
if(!process.server) -
你是怎么解决的,我没有那个标记的解决方案
-
@ManishPatidar 只需遵循以下解决方案即可。如果您真的关心 seo,您必须在服务器端呈现页面,请不要使用 localforage
标签: javascript vue.js nuxt.js server-side-rendering