vue使用bootstrap

这篇博客介绍了如何在Vue项目中整合Bootstrap。首先通过npm安装jquery,并在webpack配置文件中引入,接着在main.js中加载。然后安装bootstrap和popper.js,解决依赖问题。最后在webpack配置中添加Popper.js,并在项目中测试Bootstrap的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、安装jquery

npm install jquery –save-dev

打开build文件夹下的webpack.base.conf.js
头部添加代码:

const webpack = require("webpack");

module.exports 中添加代码

plugins: [
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery"
    })
  ],

在入口文件main.js中加入代码:

import $ from 'jquery' ;

测试:

<script>
  $(function () {
    alert()
  })
export default {
  name: 'HelloWorld',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

2、安装bootstrap

npm install –save-dev bootstrap

在入口文件main.js中加入代码:

import '../node_modules/bootstrap/dist/css/bootstrap.min.css'
import '../node_modules/bootstrap/dist/js/bootstrap.min'

这时可能会报错

 ERROR  Failed to compile with 1 errors                                                                                                                                                                                                                 14:43:35

This dependency was not found:

* popper.js in ./node_modules/_bootstrap@4.1.3@bootstrap/dist/js/bootstrap.min.js

To install it, you can run: npm install --save popper.js

因为bootstrap.js不仅依赖jquery还有popper.js,所以安装popper.js

cnpm install –save popper.js

同时在webpack.base.conf.js加入》》》Popper: [‘popper.js’, ‘default’],

plugins: [
    new webpack.ProvidePlugin({
      $:"jquery",
      jQuery:"jquery",
      "windows.jQuery":"jquery",
      Popper: ['popper.js', 'default'],
    })
  ],

测试加入bootstrap代码

   <div class="btn-group" role="group" aria-label="...">
      <button type="button" class="btn btn-default">Left</button>
      <button type="button" class="btn btn-default">Middle</button>
      <button type="button" class="btn btn-default">Right</button>
    </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值