解决ssr服务端渲染程序启动报错: ReferenceError: location is not defined

现象:

原因:chatgpt给出的解释很到位:

该错误表明代码尝试访问 location 对象,该对象通常在浏览器环境中可用。

然而,你的服务器端代码正在 Node.js 环境中运行而在这个环境中 location 对象未定义

问题可能与你在服务器端使用的 Vue Router 代码有关。Vue Router 中的 createWebHashHistory 函数试图访问 location 对象,导致了这个错误。

为解决这个问题,你应该在服务器端使用不同的 history 模式。对于服务器端渲染(SSR),通常使用 createMemoryHistory 而不是 createWebHashHistory

The error you're encountering, ReferenceError: location is not defined, indicates that the code is trying to access the location object, which is typically available in a browser environment. However, your server-side code is running in a Node.js environment, where the location object is not defined.

The issue is likely related to the Vue Router code that you're using on the server side. The createWebHashHistory function from Vue Router is attempting to access the location object, which is causing the error.

To resolve this issue, you should use a different history mode on the server side. For server-side rendering (SSR), it's common to use createMemoryHistory instead of createWebHashHistory or createWebHistory. Here's how you can modify your code:

但是chatgpt给出的代码不大对:

// Change this line in your server.js file
const { createMemoryHistory } = require('vue-router');

// ...

server.get("*", async (req, res) => {

  // 这样写是没有用的
  const { app, router, store } = await App(req, createMemoryHistory());

  // ...
});
 

 解决办法: 在原始router路由定义文件中,根据SSR的环境变量动态确定使用的history模式

ssr环境使用history模式为:createMemoryHistory

浏览器环境使用history模式为:createWebHashHistory

通过VUE_APP_SSR环境变量,利用三元表达式动态确定history的模式

const router = createRouter({

  history: process.env.VUE_APP_SSR

    ? createMemoryHistory()

    : createWebHashHistory(process.env.BASE_URL),

  routes,

});

 VUE_APP_SSR环境变量通过script脚本执行时传入

  • 22
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用中提到的解决方法是在vue.config.js中添加一些配置,其中使用了webpack的ProvidePlugin插件来提供全局的jquery变量。这样就能解决报错"ReferenceError: $ is not defined"的问题。中提到的报错提示不是由于没有使用lib:["dom"]造成的,而是在类型注释中使用了MouseEvent。在这种情况下,没有问题的。中指出,如果项目中引用了.eslintrc.js文件,还需要在文件的module.exports中为env添加一个键值对jquery: true,然后重新启动项目。这样也可以解决报错"ReferenceError: $ is not defined"的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Vue引入JQuery报错caught ReferenceError: jQuery is not defined](https://blog.csdn.net/qq_46034741/article/details/130032090)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [vue ssr 报错 ReferenceError: MouseEvent is not defined / window is not defined 等等](https://blog.csdn.net/qq_28550263/article/details/130152398)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Vue小知识: $ is not defined错误解决](https://blog.csdn.net/weixin_43945983/article/details/88294052)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值