使用插件Vue.js时出现错误“未定义窗口”

时间:2019-07-24 16:27:07

标签: vue.js plugins nuxt.js

只需学习即可。从以下位置为网站安装滑块插件:https://github.com/surmon-china/vue-awesome-swiper。转移了代码并收到了这样的错误:“未定义窗口”下面的代码。我使用nuxt.js。互联网上有几种解决方案,但没有一个对我有帮助。

slider.vue

<script>
import 'swiper/dist/css/swiper.css'

import { swiper, swiperSlide } from 'vue-awesome-swiper';

  if (process.browser) {
    const VueAwesomeSwiper = require('vue-awesome-swiper/dist/ssr')
    Vue.use(VueAwesomeSwiper)
  }

  export default {
    components: {
    swiper,
    swiperSlide
  },
    data() {
      return {
        swiperOption: {
          slidesPerView: 'auto',
          centeredSlides: true,
          spaceBetween: 30,
          pagination: {
            el: '.swiper-pagination',
            clickable: true
          }
        }
      }
    }
  }
</script>

vue-awesome-swiper.js

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'

// require styles
import 'swiper/dist/css/swiper.css'

Vue.use(VueAwesomeSwiper,{});

nuxt.config.js

module.exports = {
    /*
    ** Headers of the page
    */
    head: {
        title: 'stud-cit',
        meta: [
            { charset: 'utf-8' },
            { name: 'viewport', content: 'width=device-width, initial-scale=1' },
            { hid: 'description', name: 'description', content: 'Stud-cit site' }
        ],
        link: [
            { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
        ]
    },

    loading: { color: 'blue'},
    plugins: [
        '~/plugins/vuetify',
        '~/plugins/vue-awesome-swiper' ,
        '~/plugins/vuePose'
    ],
    build: {
        vendor :['vue-awesome-swiper/dist/ssr'],
        extend (config, { isDev, isClient }) {
            if (isDev && isClient) {
                config.module.rules.push({
                    enforce: 'pre',
                    test: /\.(js|vue)$/,
                    loader: 'eslint-loader',
                    exclude: /(node_modules)/
                });
            }

        }
    }
};

1 个答案:

答案 0 :(得分:1)

此库具有用于SSR的特殊构建。 Reference

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper/dist/ssr'

Vue.use(VueAwesomeSwiper)