我通过vue init创建了一个新的Vue项目我添加了bootstrap 4 with yarn add bootstrap@4.0.0-alpha.6
在main.js中我尝试导入bootstrap和jquery:
import Vue from 'vue'; import jQuery from 'jquery'; import bootstrap from 'bootstrap'; import App from './App'; import router from './router';
但我得到:
Uncaught Error: Bootstrap’s JavaScript requires jQuery. jQuery must be
included before Bootstrap’s JavaScript.
window.jQuery = window.$= $;不起作用
最后,我在哪里以及如何加载Sass以便整个应用程序都可以使用它?
解决方法
>安装Bootstrap及其依赖项,jQuery和Popper.js:
npm install bootstrap@4.0.0-beta popper.js jquery –save-dev
>编辑你的/build/webpack.dev.conf.js文件,为jQuery和Popper.js添加一个插件来处理依赖项(你可以在Bootstrap文档中阅读更多关于here的信息):
插件:[
…
new webpack.ProvidePlugin({
$:’jquery’,
jQuery:’jquery’,
‘window.jQuery’:’jquery’,
Popper:[‘popper.js’,’default’],
//如果你单独导入插件,你还必须在这里要求它们:
Util:“exports-loader?Util!bootstrap / js / dist / util”,
下拉列表:“exports-loader?Dropdown!bootstrap / js / dist / dropdown”,
…
})
…
]
>编辑/src/main.js将Bootstrap导入应用程序的入口点:
从’vue’导入Vue
从’./App’导入应用程序
从’./router’导入路由器
导入’bootstrap’
>编辑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>
>从CLI运行您的Vue应用程序
npm开始
I had to add the @import rule after the #app selector,otherwise the scoped styling would be canceled out by the Bootstrap import. I think there is a better way to do this so I will update my answer once I figure it out.
