简体   繁体   English

将引导程序添加到 Vue CLI 项目

[英]Adding bootstrap to Vue CLI project

I'm new to Vue and webpack in general and I'm having a hard time figuring out how to import things.一般来说,我是 Vue 和 webpack 的新手,我很难弄清楚如何导入东西。

I created a fresh Vue project through vue init I added bootstrap 4 with yarn add bootstrap@4.0.0-alpha.6我通过vue init创建了一个新的 Vue 项目我用yarn add bootstrap@4.0.0-alpha.6添加了 bootstrap 4

In main.js I try to import bootstrap and jquery:main.js我尝试导入引导程序和 jquery:

import Vue from 'vue';
import jQuery from 'jquery';
import bootstrap from 'bootstrap';
import App from './App';
import router from './router';

But I get:但我得到:

Uncaught Error: Bootstrap's JavaScript requires jQuery.未捕获的错误:引导程序的 JavaScript 需要 jQuery。 jQuery must be included before Bootstrap's JavaScript. jQuery 必须包含在 Bootstrap 的 JavaScript 之前。

window.jQuery = window.$ = $; does not work工作

Finally, where and how do I load the Sass such that it's available to the whole app?最后,我在哪里以及如何加载 Sass 以使其可用于整个应用程序?

I was having the same issue and this is what I ended up with however, I didn't use Bootstrap alpha since the beta has since been released.我遇到了同样的问题,这就是我最终得到的结果,自从测试版发布以来,我没有使用 Bootstrap alpha。


  1. Install Bootstrap along with its dependencies, jQuery and Popper.js:安装 Bootstrap 及其依赖项,jQuery 和 Popper.js:
npm install bootstrap@4.0.0-beta popper.js jquery --save-dev
  1. Edit your /build/webpack.dev.conf.js file to add a plugin for jQuery and Popper.js to deal with dependencies (you can read more about that in the Bootstrap docs ):编辑您的/build/webpack.dev.conf.js文件,为 jQuery 和 Popper.js 添加一个插件来处理依赖项(您可以在Bootstrap 文档中阅读更多相关信息):
plugins: [
    // ...
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
        Popper: ['popper.js', 'default'],
        // In case you imported plugins individually, you must also require them here:
        Util: "exports-loader?Util!bootstrap/js/dist/util",
        Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
        // ...
    })
    // ...
]
  1. Edit /src/main.js to import Bootstrap into the app's entry point:编辑/src/main.js以将 Bootstrap 导入应用程序的入口点:
import Vue from 'vue'
import App from './App'
import router from './router'
import 'bootstrap' // ←
  1. Edit the App.vue' file's <style> portion to import Bootsrap into your app's root css:编辑 App.vue 文件的<style>部分以将 Bootsrap 导入到应用程序的根 css 中:
<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</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;
}

@import'~bootstrap/dist/css/bootstrap.css'
</style>
  1. Run your Vue app from the CLI从 CLI 运行您的 Vue 应用程序
npm start

Vue.js 项目的屏幕截图


I had to add the @import rule after the #app selector, otherwise the scoped styling would be canceled out by the Bootstrap import.我必须在 #app 选择器之后添加 @import 规则,否则 Bootstrap 导入会取消范围样式。 I think there is a better way to do this so I will update my answer once I figure it out.我认为有更好的方法可以做到这一点,所以一旦我弄清楚了,我会更新我的答案。

You don't want to do any kind of adjustment in webpack.config.js like adding jQuery global in plugin array.你不想在webpack.config.js做任何调整,比如在插件数组中添加 jQuery 全局。 Once you installed all bootstrap dependencies like jQuery and popper than just import them in app.vue or main.js (you can import separately in each component if you wish)一旦你安装了所有的bootstrap依赖,比如jQuerypopper不是仅仅在app.vuemain.js import它们(如果你愿意,你可以在每个组件中单独导入)

import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/src/jquery.js'
import 'bootstrap/dist/js/bootstrap.min.js'

thats it ..就是这样 ..

With Vue CLI v3.x/v4.x, you can also use the @techiediaries/vue-cli-plugin-bootstrap to quickly add the latest Bootstrap 4 version in your Vue project without manually adding any files or settings.使用 Vue CLI v3.x/v4.x,您还可以使用@techiediaries/vue-cli-plugin-bootstrap快速在您的 Vue 项目中添加最新的 Bootstrap 4 版本,而无需手动添加任何文件或设置。

Head over to a new command-line interface, navigate to your project's folder and run the following command:转到新的命令行界面,导航到您的项目文件夹并运行以下命令:

$ vue add @techiediaries/bootstrap

The plugin will take care of adding any configuration options and install the dependencies into your project so you can start using Bootstrap classes right away without spending time figuring out what settings or dependencies you need to add.该插件将负责添加任何配置选项并将依赖项安装到您的项目中,这样您就可以立即开始使用 Bootstrap 类,而无需花时间弄清楚需要添加哪些设置或依赖项。

I will try to be as precise as possible我会尽量准确

1.Go the the Vue Cli project run : 1.Go Vue Cli 项目运行:

npm i bootstrap jquery popper.js

2.Go to src/main.js import like: 2.转到src/main.js导入,如:

import 'bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css';

Only For Bootstrap 5仅适用于 Bootstrap 5

Follow the below in cmd按照下面的cmd

  1. vue create project-name vue 创建项目名称

  2. Select Vue version选择Vue版本

  3. cd project-name cd 项目名称

  4. npm i bootstrap @popperjs/core (just bootstrap means latest available bootstrap version) npm i bootstrap @popperjs/core (只是引导意味着最新的可用引导版本)

     (or)

    i)npm i bootstrap (to install bootstrap) i)npm i bootstrap(安装引导程序)

    ii)npm install --save @popperjs/core (to install popper.js) ii)npm install --save @popperjs/core(安装popper.js)

    Note: Try this if only point 4 emits error注意:如果只有第 4 点发出错误,请尝试此操作

  5. Add the below to src/main.js import 'bootstrap';将以下内容添加到 src/main.js import 'bootstrap'; import 'bootstrap/dist/css/bootstrap.min.css';导入 'bootstrap/dist/css/bootstrap.min.css';

  6. Done now bootstrap will work on Vue-Cli现在完成 bootstrap 将在 Vue-Cli 上工作

For Gridsome users, the solution is exactly the same!对于Gridsome用户,解决方案完全一样!

yarn add jquery bootstrap popper.js
import 'jquery/src/jquery.js';
import 'popper.js/dist/popper.min.js';
import 'bootstrap/dist/js/bootstrap.min.js';

You can also import the css here like this import 'bootstrap/dist/css/bootstrap.min.css' however bootstrap provides a SCSS file which can be highly customisable, if you want this kind of customisation, import in the main.js file a new scss file您也可以像这样import 'bootstrap/dist/css/bootstrap.min.css'在这里import 'bootstrap/dist/css/bootstrap.min.css'但是 bootstrap 提供了一个可以高度自定义的 SCSS 文件,如果您想要这种自定义,请在main.js文件中导入一个新的 scss 文件

// Load core styling
import '~/assets/styles/core.scss';

and then import bootstrap like this然后像这样导入引导程序

@import '~bootstrap/scss/bootstrap';
@import 'theme.scss';

The theme.scss follows this document https://getbootstrap.com/docs/4.0/getting-started/theming/ theme.scss 遵循本文档https://getbootstrap.com/docs/4.0/getting-started/theming/

根据您已经安装的软件包(例如 Vuetify),您可能无法使用 boostrap 4 + vue 3。在按照线程中的说明操作之前,请检查您的版本以确认!

As usual in JS ecosystem the answers get out of date very quick.. this is what is working for my right now:与 JS 生态系统中的往常一样,答案很快就会过时。这就是我现在的工作:

$ yarn add jquery bootstrap @popperjs/core
├─ @popperjs/core@2.11.5
├─ bootstrap@5.2.0
└─ jquery@3.6.0
// main.js
import '@popperjs/core/dist/umd/popper';
import 'bootstrap/dist/css/bootstrap.min.css'
import 'jquery/src/jquery.js'
import 'bootstrap/dist/js/bootstrap.min.js'

Brother, Greetings!兄弟,您好! Simple way to install and configure Bootstrap in your Vue project is thatVue 项目中安装和配置 Bootstrap的简单方法是

  1. First install bootstrap using a command in the terminal of the IDE which is below:首先使用 IDE 终端中的命令安装引导程序,如下所示:

    npm i bootstrap npm i 引导

  2. You need to configure this in your main.js file so that it is applied to the whole project.您需要在 main.js 文件中配置它,以便将其应用于整个项目。 You need to import this from node_modules folder to use it in project.您需要从node_modules文件夹中导入它才能在项目中使用它。

    import "../node_modules/bootstrap/dist/css/bootstrap.css";导入“../node_modules/bootstrap/dist/css/bootstrap.css”;

    import "../node_modules/bootstrap/dist/js/bootstrap.bundle";导入“../node_modules/bootstrap/dist/js/bootstrap.bundle”;

Just paste these lines in your main.js file because this is the path where your required bootstrap files will be if not then check the path and edit this path.只需将这些行粘贴到您的main.js文件中,因为这是您所需的引导文件所在的路径,如果不是,则检查路径并编辑此路径。

If you want to test whether it has worked or not, go to the app component and create a button , provide it a bootstrap class and run the project to see whether it is working or not.如果您想测试它是否工作,go 到应用程序组件创建一个按钮,为其提供引导程序 class并运行项目以查看它是否工作。

Thanks, take care:)谢谢,保重:)

CLIC ON THIS LINK TO SEE THAT It works for me单击此链接以查看它对我有用

use Bootstrap CDN and include it as a in your index.html使用Bootstrap CDN 并将其包含在 index.html 中

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

I never used Bootsrap with Vue.js, as I'm not a fan.我从来没有在 Vue.js 中使用过 Bootsrap,因为我不是它的粉丝。 But when I used Bulma's sass, first I did npm install for Bulma.但是当我使用 Bulma 的 sass 时,我首先为 Bulma 做了npm install Then I created a folder in src/assets/sass and inside it created a main.scss and inside it I imported bulma by @import '~bulma/bulma';然后我在src/assets/sass创建了一个文件夹,并在其中创建了一个main.scss并在其中通过@import '~bulma/bulma';导入了 bulma @import '~bulma/bulma';

jQuery is not required in current version (Right now: "bootstrap": "^4.4.1", "bootstrap-vue": "^2.5.0")当前版本不需要 jQuery(现在:“bootstrap”:“^4.4.1”,“bootstrap-vue”:“^2.5.0”)

  • install bootstrap安装引导程序
    • npm install bootstrap-vue bootstrap --save
  • Then, register BootstrapVue in your app entry point (main.js)然后,在您的应用程序入口点 (main.js) 中注册 BootstrapVue
    • import BootstrapVue from 'bootstrap-vue/dist/bootstrap-vue.esm';
    • Vue.use(BootstrapVue)
  • And import Bootstrap and BootstrapVue css files in main.js:并在 main.js 中导入 Bootstrap 和 BootstrapVue css 文件:
    • import 'bootstrap/dist/css/bootstrap.css'
    • import 'bootstrap-vue/dist/bootstrap-vue.css'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM