【问题标题】:Bootstrap Carousel in Vue stops working after navigating to another route and coming back导航到另一条路线并返回后,Vue 中的 Bootstrap Carousel 停止工作
【发布时间】:2022-04-13 15:38:41
【问题描述】:

我在我的(单页)Vue 3 应用程序中使用来自 Bootstrap 5.0.2 的轮播。当我最初打开页面时,轮播工作正常。但是,当我使用 Vue 路由器导航到另一个视图并返回时,Carousels 自动幻灯片效果停止工作。控件仍在工作,但不再有自动转换。我可以想象这与 Vue 从 DOM 中删除整个组件并稍后再次附加有关,但我不知道如何解决这个问题。

<template>
  <div class="welcome">
    <div id="carouselExampleSlidesOnly" class="carousel slide carousel-fade" data-bs-ride="carousel"
         data-bs-pause="false" data-bs-interval="3000" style="width: 600px" >
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img src="../assets/welcome_slideshow/dummy_1.jpg" class="d-block w-100">
        </div>
        <div class="carousel-item">
          <img src="../assets/welcome_slideshow/dummy_2.jpg" class="d-block w-100">
        </div>
        <div class="carousel-item">
          <img src="../assets/welcome_slideshow/dummy_3.jpg" class="d-block w-100">
        </div>
      </div>
      <!-- controls -->
      <button class="carousel-control-prev" type="button" data-bs-target="#carouselExampleSlidesOnly" data-bs-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Previous</span>
      </button>
      <button class="carousel-control-next" type="button" data-bs-target="#carouselExampleSlidesOnly" data-bs-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Next</span>
      </button>
      -->
    </div>
  </div>
</template>

【问题讨论】:

    标签: vue-router vuejs3 bootstrap-5


    【解决方案1】:

    请注意,您使用的是 BS5。我最近遇到了这个确切的问题! 不是特别的解决方案,但经过很多痛苦

      mounted () {
        // Bootstrap x Vue bug that stops the carousel working on navigating
        new bootstrap.Carousel($("#exampleSelector")).cycle()
      },
    在你的出口部分敲这个。

    这让它每次都能工作,有点笨拙,但它是目前可行的解决方法。知道有点晚了,但看到你是唯一一个遭受这种痛苦的人,也许它会帮助别人!

    认为这是 Boostrap 或 Vue 可能同时存在的问题。

    【讨论】:

    • 我记得在寻找解决方案后,我使用了类似计时器的东西来初始化或重新启动轮播。所以它与您的解决方案相似,但使用安装的钩子应该比使用计时器更好。因此,我会将您的答案标记为已接受。我还想提一下,我最终将整个引导轮播重新构建为一个简单的 VUE 组件,这比预期的要容易,并且对于我的特定用例来说就像一个魅力。
    猜你喜欢
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2020-02-29
    • 2019-03-18
    • 1970-01-01
    相关资源
    最近更新 更多