【问题标题】:How to use "v-owl-carousel" with nuxt and vuetify?如何在 nuxt 和 vuetify 中使用“v-owl-carousel”?
【发布时间】:2021-08-22 11:36:02
【问题描述】:

我正在开发 Nuxt 应用程序并使用“Vuetify”作为我的前端框架。在我的一个页面中,我决定使用"v-owl-carousel" 来制作轮播。该页面的代码如下:

<template>
<v-container>
<v-row>
    <v-col cols="12">
    <client-only> <!-- important to add no-ssr-->
    
    <carousel :autoplay="true" :number="6">
        <v-card>
            <img :src="require(`~/assets/imgs/books/07.webp`)">
        </v-card>

        <v-card>
            <img :src="require(`~/assets/imgs/books/02.webp`)">
        </v-card>

        <v-card>
            <img :src="require(`~/assets/imgs/books/03.webp`)">
        </v-card>

        <v-card>
            <img :src="require(`~/assets/imgs/books/04.webp`)">
        </v-card>

        <v-card>
            <img :src="require(`~/assets/imgs/books/05.webp`)">
        </v-card>

        <v-card>
            <img :src="require(`~/assets/imgs/books/06.webp`)">
        </v-card>
    </carousel>
    
    </client-only>
    </v-col>
</v-row>
</v-container>
</template>

这里的问题是所有图像中只有 3 张图像显示在轮播中,当轮播循环播放时,它什么都不会显示,直到它返回到那 3 张图像。我发现如果我在 default.vue 布局中像下面的代码那样注释“v-app”,它可以正常工作:

<template>
<!--
<v-app dark lang="fa" dir="rtl">
  <the-nanigation />
  <v-main>
-->
    <div id="mainContent">
      <nuxt />
    </div>
<!--
  </v-main>
</v-app>
-->

</template>

但根据Vuetify documentation,我们必须在我们的应用程序中使用v-app。那么在我使用 Vuetify 的应用中使用 v-owl-carousel 的解决方案是什么。

【问题讨论】:

  • 我不建议使用 owl-carouselas of today: stackoverflow.com/a/68578130/8816585 最好的想法是使用更现代的东西,并与我们分享一个 Github repo 与您的尝试。
  • @kissu 我阅读了您在评论中添加的链接。我想使用 swipper.js,但在其文档中说它与 Vue3 兼容。我也试过“VueSlickCarousel”。但是同样存在 v-app 的问题。你自己有没有用 nuxt 测试过任何支持多张幻灯片并且对 Vuetify 没有任何问题的轮播包?
  • 与 Vue3 的兼容性并不总是意味着,只有 Vue3 。既然我在回答中提出了建议,那是因为它与 Nuxt (Vue2) 完美配合。我个人也确实将它与 Buefy 和 Tailwind 一起使用,基本上,两者都不是问题,因为这两个正在做两件完全不同的事情。你能尝试用这 2 个(Vuetify 和 awesome-slider 一个)让我们成为一个 Github 存储库吗?
  • @kissu 我尝试使用您的答案,但我无法使用 Yarn,也许我的互联网或系统有问题,但感谢您的帮助。我终于用Vuetify slide-group解决了我的问题。
  • @kissu,虽然我无法尝试您的答案,但由于您的时间和精力,我投了赞成票。

标签: vue.js nuxt.js vuetify.js carousel


【解决方案1】:

好的,我在这个 github repo 上为你做了这件事:https://github.com/kissu/so-vuetify-awesome-swiper

主要是这个
nuxt.config.js文件

import colors from 'vuetify/es5/util/colors'

export default {
  ssr: false,
  target: 'static',
  head: {
    titleTemplate: '%s - so-vuetify-awesome-swiper',
    title: 'so-vuetify-awesome-swiper',
    htmlAttrs: {
      lang: 'en',
    },
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: '' },
      { name: 'format-detection', content: 'telephone=no' },
    ],
    link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }],
  },

  components: true,

  buildModules: ['@nuxtjs/eslint-module', '@nuxtjs/vuetify'],

  modules: ['@nuxtjs/axios'],

  vuetify: {
    customVariables: ['~/assets/variables.scss'],
    theme: {
      dark: true,
      themes: {
        dark: {
          primary: colors.blue.darken2,
          accent: colors.grey.darken3,
          secondary: colors.amber.darken3,
          info: colors.teal.lighten1,
          warning: colors.amber.base,
          error: colors.deepOrange.accent4,
          success: colors.green.accent3,
        },
      },
    },
  },
}

这个package.json

{
  "name": "so-vuetify-awesome-swiper",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "dev": "nuxt",
    "build": "nuxt build",
    "start": "nuxt start",
    "generate": "nuxt generate",
    "lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
    "lint": "yarn lint:js"
  },
  "dependencies": {
    "@nuxtjs/axios": "^5.13.6",
    "core-js": "^3.15.1",
    "nuxt": "^2.15.7",
    "swiper": "^6.8.4",
    "vue-awesome-swiper": "^4.1.1",
    "vuetify": "^2.5.5"
  },
  "devDependencies": {
    "@babel/eslint-parser": "^7.14.7",
    "@nuxtjs/eslint-config": "^6.0.1",
    "@nuxtjs/eslint-module": "^3.0.2",
    "@nuxtjs/vuetify": "^1.12.1",
    "eslint": "^7.29.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-nuxt": "^2.0.0",
    "eslint-plugin-prettier": "^3.4.1",
    "eslint-plugin-vue": "^7.12.1",
    "prettier": "^2.3.2"
  }
}

这是我创建的.vue 组件

<template>
  <div>
    <p>Amazin swiper huh? ?</p>
    <swiper :options="swiperOptions" class="swiper">
      <swiper-slide v-for="image in gallery" :key="image.id">
        <img :src="image.src" />
      </swiper-slide>

      <div
        v-show="prevSlideAvailable"
        slot="button-prev"
        @click="$refs.swiper.$swiper.slidePrev()"
      ></div>
      <div
        v-show="nextSlideAvailable"
        slot="button-next"
        @click="$refs.swiper.$swiper.slideNext()"
      ></div>
    </swiper>
  </div>
</template>

<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'
import 'swiper/swiper-bundle.css'

export default {
  name: 'AwesomeSwiper',
  components: {
    Swiper,
    SwiperSlide,
  },
  data() {
    return {
      gallery: [
        { id: 1, src: 'https://source.unsplash.com/random/500x500?sig=1' },
        { id: 2, src: 'https://source.unsplash.com/random/500x500?sig=2' },
        { id: 3, src: 'https://source.unsplash.com/random/500x500?sig=3' },
      ],
      swiperOptions: {
        slidesPerView: 1,
        spaceBetween: 30,
        lazy: false,
        effect: 'fade',
        pagination: {
          el: '.swiper-pagination',
          type: 'progressbar',
        },
        navigation: {
          disabledClass: '.nice-meme',
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
      },
    }
  },
}
</script>

<style scoped>
.swiper {
  width: 500px;
  margin: 0 auto;
}
</style>

到目前为止,结果看起来很棒

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多