Swiper Vue组件事件处理程序在Nuxt中不工作

hpxqektj  于 2022-12-14  发布在  Vue.js
关注(0)|答案(1)|浏览(123)

我在我的Nuxt项目中使用Swiper.js。我使用docs提供的默认代码。我尝试记录swiper示例,但它不工作。在控制台中,它给我以下错误

下面是我的模板代码和onSwiper函数

<template>
    <h1>Slider</h1>
    <div class="container">

        <swiper @swiper="onSwiper" :navigation="true" :slides-per-view="3" :space-between="89" :loop="true"
            :loopFillGroupWithBlank="true">
            <swiper-slide v-for="image in images" :key="image">
                <card :image="image" />
            </swiper-slide>

        </swiper>
        <button @click="slideTo(4)" class="bg-primary btn-primary"> Next Slide</button>

    </div>
</template>

<script>
import {
    Swiper,
    SwiperSlide,
    useSwiper
} from 'swiper/vue';
import SwiperCore, {
    Navigation
} from 'swiper';
import 'swiper/css';
import "swiper/css/navigation";

SwiperCore.use([Navigation]);

export default {
    name: "Slider",
    components: {
        Swiper,
        SwiperSlide,
    },
    setup() {
        const onSwiper = (swiper) => {
            console.log(swiper);
        };
    },
    data() {
        return {
            images: [
                "https://cdn.pixabay.com/photo/2015/12/12/15/24/amsterdam-1089646_1280.jpg",
                "https://cdn.pixabay.com/photo/2016/02/17/23/03/usa-1206240_1280.jpg",
                "https://cdn.pixabay.com/photo/2022/02/09/03/48/oriental-garden-lizard-7002565_960_720.jpg",
                "https://cdn.pixabay.com/photo/2016/12/04/19/30/berlin-cathedral-1882397_1280.jpg",
                "https://cdn.pixabay.com/photo/2015/12/12/15/24/amsterdam-1089646_1280.jpg"
            ],
            

        };
    },
   

};
</script>

Package.json

{
  "private": true,
  "scripts": {
    "build": "nuxt build",
    "dev": "nuxt dev",
    "generate": "nuxt generate",
    "preview": "nuxt preview",
    "postinstall": "nuxt prepare"
  },
  "devDependencies": {
    "autoprefixer": "^10.4.13",
    "nuxt": "3.0.0",
    "postcss": "^8.4.19",
    "tailwindcss": "^3.2.4"
  },
  "dependencies": {
    "@nuxtjs/google-fonts": "^2.0.0",
    "@pinia/nuxt": "^0.4.6",
    "@tailwindcss/typography": "^0.5.8",
    "swiper": "^8.4.5"
  }
}

下一步配置

// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  css: ["~/assets/css/tailwind.css"],
  modules: ["@pinia/nuxt"],
  postcss: {
    plugins: {
      tailwindcss: {},
      autoprefixer: {},
    },
  },
});
sr4lhrrt

sr4lhrrt1#

你需要在最后return它,如他们的例子所示。

setup() {
  const onSwiper = (swiper) => {
    console.log(swiper);
  }

  return {
    onSwiper
  }
}

只有script setup会自动传回函数/变数。

相关问题