vue使用Bootstrap的详细方法

1,161 阅读2分钟

要在Vue中使用Bootstrap,可以按照以下步骤进行操作:

  1. 首先先创建vue项目(vue2和Vue3都可以)
Vue create bootstrap-vue-app
cd bootstrap-vue-app
npm run serve
  1. 安装Bootstrap:首先,需要安装Bootstrap。可以使用npm或者yarn来安装Bootstrap。打开终端,并在项目的根目录中运行以下命令:
npm install bootstrap

或者

yarn add bootstrap
  1. 导入Bootstrap样式:在Vue项目中,可以在main.js文件中导入Bootstrap的样式文件。打开main.js文件,并添加以下代码:
import 'bootstrap/dist/css/bootstrap.css'

请注意,可能还需要在Vue组件中导入其他Bootstrap的JavaScript文件,以便使用一些需要JavaScript支持的组件,如弹出框、标签页等。可以在需要使用这些组件的Vue组件中导入相关的JavaScript文件。

main.js

import 'bootstrap/dist/css/bootstrap.js

5.用Bootstrap组件:现在,可以在Vue组件中使用Bootstrap的组件和样式了。例如,可以在Vue组件的模板中使用Bootstrap的导航组件

<template>
  <nav class="navbar navbar-expand-lg bg-body-tertiary">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a class="nav-link active" aria-current="page" href="#">Home</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
            Dropdown
          </a>
          <ul class="dropdown-menu">
            <li><a class="dropdown-item" href="#">Action</a></li>
            <li><a class="dropdown-item" href="#">Another action</a></li>
            <li><hr class="dropdown-divider"></li>
            <li><a class="dropdown-item" href="#">Something else here</a></li>
          </ul>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled">Disabled</a>
        </li>
      </ul>
      <form class="d-flex" role="search">
        <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success" type="submit">Search</button>
      </form>
    </div>
  </div>
</nav>
</template>

[Bootstrap-Vue](BootstrapVue中文文档 - itxst.com)是Vue.js的一个开源工具包,它可以让你在应用程序中使用Bootstrap的本地组件,而不需要使用jQuery。这个库一直在更新,以支持最新的vue.js功能,并提供与新版本Bootstrap的兼容性。

1建一个vue项目

如果使用的是vue-cli 可以简单的使用下面的命令创建项目

//创建项目
vue create itxst  
//创建成功后输入下面命令进入目录
cd itxst

注意:只能使用vue2项目,使用Vue3的时候会报错

2添加Bootstrap-vue插件

npm install bootstrap-vue bootstrap

3使用BootstrapVue

//在main.js文件全局注册BootstrapVue组件 
import Vue from 'vue' 
import App from './App.vue'
Vue.config.productionTip = false 
//全局注册BootstrapVue组件 
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue' 
//引用BootstrapVue样式 
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css' 
// Install BootstrapVue
Vue.use(BootstrapVue) 
// Optionally install the BootstrapVue icon components plugin 
Vue.use(IconsPlugin) 
new Vue({
render: h => h(App),
}).$mount('#app')

使用BootstrapVue的按钮组件

<template>
    <div >
        <b-button>Button</b-button> 
        <b-button variant="danger">Button</b-button> 
        <b-button variant="success">Button</b-button> 
        <b-button variant="outline-primary">Button</b-button>
    </div>
</template> 
<script> 
    export default { 
    name: 'itxst.com',
    props: { msg: String } }
</script>
<!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> button+button{ margin-left: 6px; } </style>