我的第一个 Vue.js (3) 应用程序使用 Bootstrap-vue (5),但弹出窗口不起作用

问题描述 投票:0回答:1

我目前正在学习 Vue.js,并且对 Bootstrap 有一些经验。当我看到你可以将 Bootstrap 与 Vue.js 一起使用时,我想我应该尝试一下。我的代码相当简单,一个下拉菜单和一个弹出窗口。下拉菜单有效,但弹出窗口无效。我正在使用 Bootstrap 5 和 Vue.js 3。我还安装了 popper-js/core。我的 main.js 文件需要更多导入吗?顺便说一句,我使用 vue/cli 来创建我的项目。

app.vue

<template>
  <img alt="Vue logo" src="./assets/logo.png">
  <HelloWorld msg="Welcome to Your Vue.js App"/>
  <h1>Bootstrap</h1>
    <div class="container">
      <div class="btn-group">
        <button type="button" class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
          Action
        </button>
        <ul class="dropdown-menu">
          <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>
          <li><hr class="dropdown-divider"></li>
          <li><a class="dropdown-item" href="#">Separated link</a></li>
        </ul>
      </div>  
      <button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="top" data-bs-content="Top popover">Popover on top</button>
      <button type="button" class="btn btn-secondary" data-bs-container="body" data-bs-toggle="popover" data-bs-placement="right" data-bs-content="Right popover">Popover on right</button>
      <div class="text-center my-3">
        <b-button v-b-popover.hover.top="'I am popover directive content!'" title="Popover Title">Hover Me</b-button>
        <b-button id="popover-target-1">
          Hover Me
        </b-button>
        <b-popover target="popover-target-1" triggers="hover" placement="top">
          <template #title>Popover Title</template>
          I am popover <b>component</b> content!
        </b-popover>
      </div>
    </div>

</template>

<script>
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
    HelloWorld
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

main.js

import { createApp } from 'vue'
import App from './App.vue'
import 'bootstrap/dist/css/bootstrap.min.css'
import bootstrap from 'bootstrap/dist/js/bootstrap.bundle'

const app = createApp(App);

app.mount('#app');

// Initialize popovers globally
app.directive('popover', {
  mounted(el) {
    new bootstrap. Popover(el)
  }
});

npm 列表

$ npm list
[email protected] D:\projects\VueWorkspace\build-a-bot
├── @babel/[email protected]
├── @babel/[email protected]
├── @floating-ui/[email protected]
├── @popperjs/[email protected]
├── @vue/[email protected]
├── @vue/[email protected]
├── @vue/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

package.json

...
  "dependencies": {
    "@floating-ui/vue": "^1.0.6",
    "@popperjs/core": "^2.11.8",
    "bootstrap": "^5.3.3",
    "core-js": "^3.8.3",
    "vue": "^3.2.13"
  },
  "devDependencies": {
    "@babel/core": "^7.12.16",
    "@babel/eslint-parser": "^7.12.16",
    "@vue/cli-plugin-babel": "~5.0.0",
    "@vue/cli-plugin-eslint": "~5.0.0",
    "@vue/cli-service": "~5.0.0",
    "eslint": "^7.32.0",
    "eslint-plugin-vue": "^8.0.3"
  },

...

enter image description here

vuejs3 bootstrap-vue
1个回答
0
投票

Bootstrap-vue 自 2022 年以来一直处于 v2.23.0 状态,又名现已废弃的项目,不完全支持 Vue3。

您所说的 bootstrap v5 是这里的常规 CSS 框架

这两个不一样。你可以将 CSS 框架实现到 VueJS 中,但你可能已经注意到,关于整个 JS 交互部分,你确实会遇到很多问题。
您还将很难管理任何类型的状态(例如“我的模式现在打开”)。
然后,你也会遇到很多关于性能的问题,因为如果你不在所有页面上使用它的每一部分,那么加载整个 CSS 框架是没有意义的。

因此,如果您要使用某种 CSS 框架,请使用一个得到良好支持、活跃且拥有大量社区和支持的框架。

因此,为了防止您搬起石头砸自己的脚,这里有一个好的网站,其中引用了迄今为止所有好的可行的解决方案。它们都有自己的规格和用途,但您将有很多选择和更轻松的时间。他们的文档也非常明确且易于使用。

顺便说一句,这并不是一件坏事,因为所有使用 Bootstrap 的网站看起来都一样。这种设计趋势早已不复存在。

TLDR:很抱歉没有真正回答您的问题,但稍后感谢我告诉您不要碰 Boostrap。

© www.soinside.com 2019 - 2024. All rights reserved.