如何在NUXT 3页组件中导入单个Bootstrap 5 JS组件?

发布于 2025-02-10 14:38:11 字数 1472 浏览 0 评论 0原文

是否只能将单个Bootstrap 5组件导入到NUXT 3应用程序的组件中?在这种特定情况下,我想使用 下拉component 来自Bootstrap 5。需要JavaScript。

我试图这样做:

app.vue

<template>
  <div class="container mt-4">
    <ClientOnly><Dropdown /></ClientOnly>
  </div>
</template>

下拉

<template>
  <div class="dropdown">
    <button
      class="btn btn-secondary dropdown-toggle"
      type="button"
      id="dropdownMenuButton1"
      data-bs-toggle="dropdown"
      aria-expanded="false"
    >
      Dropdown button
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
  </div>
</template>

<script setup>
import dropdown from 'bootstrap/js/dist/dropdown'; //<--- how do I correctly import dropdown??
</script>

。任何人都对如何“树摇”引导JS有任何想法,只是在本地组件中导入所需的组件?

Is it possible to import just a single Bootstrap 5 component into a Nuxt 3 app's component? In this specific case, I want to use the Dropdown component from Bootstrap 5. That requires JavaScript.

I tried to do it like so:

app.vue:

<template>
  <div class="container mt-4">
    <ClientOnly><Dropdown /></ClientOnly>
  </div>
</template>

Dropdown.vue:

<template>
  <div class="dropdown">
    <button
      class="btn btn-secondary dropdown-toggle"
      type="button"
      id="dropdownMenuButton1"
      data-bs-toggle="dropdown"
      aria-expanded="false"
    >
      Dropdown button
    </button>
    <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
      <li><a class="dropdown-item" href="#">Action</a></li>
      <li><a class="dropdown-item" href="#">Another action</a></li>
      <li><a class="dropdown-item" href="#">Something else here</a></li>
    </ul>
  </div>
</template>

<script setup>
import dropdown from 'bootstrap/js/dist/dropdown'; //<--- how do I correctly import dropdown??
</script>

That did not work. Anyone have any ideas on how to "tree shake" Bootstrap JS and just import the needed components in a local component?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃气十足 2025-02-17 14:38:11

在下载页面上,您可以找到本段

源文件

通过下载我们的源SASS,JavaScript和文档文件,用自己的资产管道编译引导程序。此选项需要一些其他工具:

是否需要我们的全套构建工具将它们包括用于开发引导程序及其文档,但它们可能不适合您自己的目的。

因此,您可能会大量减少它,但是对于某些基本文件,很难排除它们,甚至可以决定是否应该被排除。

无论如何,Bootstrap Sass非常模块化,似乎只有一个组件可以创建自己的汇编。除此之外,我认为没有其他选择,您必须使用自己的汇编。

On the download page you find this paragraph:

Source files

Compile Bootstrap with your own asset pipeline by downloading our source Sass, JavaScript, and documentation files. This option requires some additional tooling:

Should you require our full set of build tools, they are included for developing Bootstrap and its docs, but they’re likely unsuitable for your own purposes.

So likely you could reduce it a lot but for some basic files it might hard to exclude them or even to decide if they should be excluded.

Anyway, bootstrap sass is quite modular and it seems to be possible to create an own compilation with only one component. Beside that I don't think that there exists another option and you had to use your own compilation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文