After transitioning my vue app to a nuxt app, I encountered an issue with vue-slick-corousel. In the original vue app, everything worked smoothly with vue-slick-carousel, but after making the necessary modifications for nuxt, the carousel wouldn't display properly on refresh. Oddly enough, if I edited the template in real time or navigated within the app without refreshing the page, the carousel worked perfectly upon revisiting the page(s). This is how I integrated vue-slick-corousel into my nuxt app:
//nuxt.config.js
plugins: [
{ src: './plugins/vue-slick-carousel.js' }
],
Here is the code from the plugin js file:
//vue-slick-carousel.js
import Vue from 'vue'
import VueSlickCarousel from 'vue-slick-carousel'
Vue.component('VueSlickCarousel', VueSlickCarousel)
And here's how it was implemented in the template:
<!-- CustomComponent.vue -->
<template>
<div>
<VueSlickCarousel :arrows="true" :dots="true">
<div v-for="(cat, index) in categories"
:key="index"
style="height: 20px; width: 10px; background-color: red; color: white"
>
{{cat.name}}
</div>
</VueSlickCarousel>
</div>
</template>
<script>
import VueSlickCarousel from 'vue-slick-carousel'
import 'vue-slick-carousel/dist/vue-slick-carousel.css'
// optional style for arrows & dots
import 'vue-slick-carousel/dist/vue-slick-carousel-theme.css'
export default {
name: 'CustomComponent',
data() {
return {
categories: []
}
},
async fetch() {
this.categories = await fetch(
'https://*****************************/categories?limit=20'
).then(res => res.json())
},
components: {
VueSlickCarousel
},
}
</script>
Upon refreshing the page, I encountered the error "TypeError: Cannot read property 'length' of undefined". However, the error disappeared when rendering the page again from the client side. I attempted enclosing the container under <client-only>
and using conditions like v-if="slik.length"
, but none of these solutions worked. How can I resolve this issue?