【发布时间】:2021-08-23 10:08:56
【问题描述】:
我正在尝试将我的 vue 应用程序转换为 nuxt 应用程序。在我的 vue 应用程序中,我轻松地使用了 vue-slick-corousel。但是对 nuxt 进行必要修改的相同代码,corousel 无法刷新。如果我实时编辑模板或在不刷新页面的情况下导航应用程序,它在第二次访问页面时可以完美运行。这是我在 nuxt 应用程序中使用 vue-slick-corousel 的方式
//nuxt.config.js
plugins: [
{ src: './plugins/vue-slick-carousel.js' }
],
插件js文件中的代码:
//vue-slick-carousel.js
import Vue from 'vue'
import VueSlickCarousel from 'vue-slick-carousel'
Vue.component('VueSlickCarousel', VueSlickCarousel)
最后在模板中:
<!-- 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>
这是我在刷新时遇到的错误“TypeError: Cannot read property 'length' of undefined”
每当我从客户端再次渲染页面时,错误就会消失。已经尝试在
下绑定容器<client-only>
尝试使用条件
v-if="slik.length"
没用。我该如何解决这个问题?
【问题讨论】:
-
如何获取
slik?这个在某些时候是未定义的,因此length的错误。这更像是一个生命周期问题。当然,它可以通过一些slik?.length来解决,但这会更好地找出为什么会发生这种情况。
标签: javascript vue.js nuxt.js