javascript - 服务器端渲染中只能在浏览器中运行的包错误
问题描述
我在我的 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;
}
});
但是我只能得到更多的错误,而且这一次页面无法渲染。
我该如何解决这个错误,我已经用谷歌搜索过了,但它没有任何帮助。
非常感谢!
(不是以英语为母语的人,如有错误请见谅)
解决方案
挂载中的延迟加载模块怎么样?
<script>
export default{
async mounted(){
const localforage = await import('localforage')
localforage.getItem('something', value => {
...
});
}
}
</script>
推荐阅读
- flutter - 如何在初始化页面之前和在flutter中运行initstate方法之前初始化静态变量
- java - Java 静态或继承变量
- ansible - 如何使用 Ansible 在 /home/{user}/ 中为所有现有用户创建子目录
- python - Django 模型不会在 celery 任务中更新
- json - 如何在火花中将Json转换为数组
- asp.net - 在 ASP.NET Core/Node JS 应用程序上安装/测试/回滚安装?
- nuxt.js - Nuxt:如何创建实现另一个 nuxt 模块的可重用函数?
- python-3.x - 当用户输入键时,将字典值增加 1
- marklogic - 使用 MarkLogic Java API 列出集合
- mongodb - 获取嵌套数组中与 MongoDB 中的条件匹配的所有元素