nuxt.js引入vue-awesome-swiper后,控制台报错window is not defined
原因:
Nuxt在服务端渲染时找不到window
查询官网文档:
解决办法:
在nuxt的plugins目录下新建vue-awesome-swiper.js文件,代码如下:import Vue from "vue";
import "swiper/dist/css/swiper.css";
if (process.browser) {
const VueAwesomeSwiper = require("vue-awesome-swiper/dist/ssr");
Vue.use(VueAwesomeSwiper);
}
修改nuxt.config.js 的plugins配置:
在nuxt.config.js的plugins里加入:
{
src: "@/plugins/vue-awesome-swiper",
ssr: false
}
IndexBanner.vue的代码为:
<div v-swiper:mySwiper="swiperOption" class="swiper">
<div class="swiper-wrapper">
<div class="swiper-slide">1</div>
<div class="swiper-slide">2</div>
</div>
<div class="swiper-pagination"></div>
</div>