I'm new to Vue and webpack in general and I'm having a hard time figuring out how to import things.
I created a fresh Vue project through vue init
I added bootstrap 4 with yarn add bootstrap@4.0.0-alpha.6
In main.js I try to import bootstrap and 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. jQuery must be included before Bootstrap's 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?
With Vue CLI v3, you can also use the vue-cli-plugin-bootstrap to quickly add the latest Bootstrap 4 version in your Vue project without manually adding any files or settings. 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.
You don't want to do any kind of adjustment in
webpack.config.js
like adding jQuery global in plugin array. Once you installed allbootstrap
dependencies likejQuery
andpopper
than justimport
them inapp.vue
ormain.js
(you can import separately in each component if you wish)thats it ..
For Gridsome users, the solution is exactly the same!
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 themain.js
file a new scss fileand then import bootstrap like this
The theme.scss follows this document https://getbootstrap.com/docs/4.0/getting-started/theming/
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.
Install Bootstrap along with its dependencies, jQuery and Popper.js:
npm install bootstrap@4.0.0-beta popper.js jquery --save-dev
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 here in Bootstrap docs):
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", ... }) ... ]
Edit /src/main.js to import Bootstrap into the app's entry point:
import Vue from 'vue' import App from './App' import router from './router' import 'bootstrap'
Edit the App.vue' file's
<style>
portion to import Bootsrap into your app's root css:Run your Vue app from the CLI
npm start
use Bootstrap CDN and include it as a in your index.html
I never used Bootsrap with Vue.js, as I'm not a fan. But when I used Bulma's sass, first I did
npm install
for Bulma. Then I created a folder insrc/assets/sass
and inside it created amain.scss
and inside it I imported bulma by@import '~bulma/bulma';
I will try to be as precise as possible
1.Go the the Vue Cli project run :
npm i bootstrap jquery popper.js
2.Go to
src/main.js
import like:import 'bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';