【问题标题】:Nuxtjs with scrollmagic gives me "window is not defined"带有 scrollmagic 的 Nuxtjs 给了我“未定义窗口”
【发布时间】:2018-05-30 09:37:51
【问题描述】:

我想在 nuxtjs 中使用 scrollmagic。

我通过 npm 安装了 scrollmagic。

npm install scrollmagic

在我添加的 nuxt.config.js 文件中

build: {
    vendor: ['scrollmagic']
},

在我的 pages/index.vue 文件中,我只是导入了它。

import ScrollMagic from 'scrollmagic'

但这只会导致这个错误

[vue-router] 无法解析异步组件默认值: ReferenceError:未定义窗口 [vue-router] 未捕获错误 在路线导航期间:ReferenceError:未定义窗口 在 C:\pathto\node_modules\scrollmagic\scrollmagic\uncompressed\ScrollMagic.js:37:2 在 C:\pathto\node_modules\scrollmagic\scrollmagic\uncompressed\ScrollMagic.js:22:20 在对象。 (C:\pathto\node_modules\scrollmagic\scrollmagic\uncompressed\ScrollMagic.js:27:2)

我该如何解决这个问题?

【问题讨论】:

    标签: window undefined scrollmagic nuxt.js


    【解决方案1】:

    在您的插件文件夹中添加一个名为“scrollmagic.js”的文件并将以下代码粘贴到其中:

    plugins/scrollmagic.js

    import ScrollMagic from 'scrollmagic'
    

    将插件添加到您的 nuxt.config.js 文件

    nuxt.config.js

    module.exports = {
      build: {
        vendor: ['scrollmagic']
      },
      plugins: [
        // ssr: false to only include it on client-side
        { src: '~/plugins/scrollmagic.js', ssr: false }
      ]
    }
    

    if (process.client) {}一起使用

    页面或组件

    <script>
    let scrollmagic
    if (process.client) {
      scrollmagic = require('scrollmagic')
    // use scrollmagic
    }
    </script>
    

    有关更多信息,请参阅有关此主题的优秀文档:https://nuxtjs.org/guide/plugins/

    【讨论】:

    • 这解决了“未定义窗口”的问题,但现在我得到了这个ERROR calling setTween() due to missing Plugin 'animation.gsap'. Please make sure to include plugins/animation.gsap.js。还没有找到 NuxtJS 的解决方案......对此有任何想法吗?
    • 您是否在插件文件夹中添加了一个名为“animation.gsap.js”的文件?如果有,你确定没有错别字吗?
    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多