注意:
await nextTick(); 如果没用,vue切换的时候可能有问题
<script lang="ts" setup> import { DArrowRight } from "@element-plus/icons-vue"; useHead({ script: [ { src: "/script/swiper.js", }, ], link: [ { rel: "stylesheet", type: "text/css", href: "/css/swiper.css", }, ], }); import { ref, onMounted, nextTick } from "vue"; import _ from "lodash"; const apiUrl = import.meta.env.VITE_API_URL; import { productCenterPageAPI } from "~/utils/api"; // 首页详情 let homeList = ref<any>([]); async function homeFun() { const home: any = await productCenterPageAPI(); homeList.value = home; await nextTick(); swiper1Container(); console.log(homeList.value, "homeFunhomeFun"); useHead({ meta: [ { name: "description", content: homeList.value.seo.des }, { hid: "keywords", name: "keywords", content: homeList.value.seo.key, }, ], }); await nextTick(); swiper1Container(); } // 轮播图 const swiper1Container = () => { const swiper = new Swiper(".swiper-container", { autoplay: { delay: 2500, disableOnInteraction: false, }, loop: true, initialSlide: 2, spaceBetween: 20, centeredSlides: true, slidesPerView: 1.9, pagination: { el: ".swiper-pagination", clickable: true, }, breakpoints: { 320: { //当屏幕宽度大于等于320 slidesPerView: 1, spaceBetween: 10, }, 768: { //当屏幕宽度大于等于768 slidesPerView: 1.8, spaceBetween: 20, }, 1280: { //当屏幕宽度大于等于1280 slidesPerView: 1.9, spaceBetween: 30, }, }, // 如果需要前进后退按钮 // navigation: { // nextEl: ".swiper-button-next", // prevEl: ".swiper-button-prev", // }, }); }; onMounted(() => { homeFun(); // console.log(`${process.env.API_URL}`, "process.env.API_URL", apiUrl); }); </script>
标签:nextTick,useHead,await,homeList,报错,import,swiper,const From: https://www.cnblogs.com/hechunfeng/p/18155159