当我在模板中包含一个本地 .js 文件时,Vue 拒绝构建,该文件引用了“window”全局 - ReferenceError: window is not defined

问题描述

我有一个 VuePress 项目,我只需要在图库的组件中包含一个本地 js 文件。 .js 文件正在引用 window 对象,而我的项目拒绝构建:

(node:57059) UnhandledPromiseRejectionWarning: ReferenceError: window is not defined
    at isMobile (4.server-bundle.js:1470:27)
    at 4.server-bundle.js:3343:20
    at 4.server-bundle.js:937:109
    at Object.345 (4.server-bundle.js:942:3)
    at __webpack_require__ (server-bundle.js:27:30)
    at Module.356 (4.server-bundle.js:5022:17)
    at __webpack_require__ (server-bundle.js:27:30)
(node:57059) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 49)
(node:57059) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

从文档中我了解到我只应该在 window 生命周期方法中引用 mounted。但是其他脚本呢?我该如何处理?

这是我如何包含脚本:

</template>
<script>
import * as GLightBox from '../assets/scripts/glightBox.js'

export default {
// ...

这是我使用它的方式:

// ...
mounted: function () {
  this.lightBox = GLightBox({
      touchNavigation: true,loop: true
  })
},

干杯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)