简体   繁体   English

Vue.js + Webpack + vue-loader + bootstrap-sass + sass-loader

[英]Vue.js + Webpack + vue-loader + bootstrap-sass + sass-loader

I'm just getting started with Vue.js + Webpack + vue-loader + bootstrap-sass + sass-loader and I'm a little lost. 我刚刚开始使用Vue.js + Webpack + vue-loader + bootstrap-sass + sass-loader,我有点失落。

What I'd like to do is use the SASS version of bootstrap with my SPA Vue.js code. 我想要做的是使用SASS版本的bootstrap和我的SPA Vue.js代码。 I want to do this so my bootstrap customisations can be done using SASS. 我想这样做,所以我的引导自定义可以使用SASS完成。 Here is what I've done: 这就是我所做的:

  • Created a new Vue.js + webpack project with vue-cli. 使用vue-cli创建了一个新的Vue.js + webpack项目。
  • Installed bootstrap-sass and sass-loader. 安装bootstrap-sass和sass-loader。
  • Added the following to build/webpack.base.conf.js: 在build / webpack.base.conf.js中添加了以下内容:

     { test: /\\.scss$/, loaders: [ 'style', 'css', 'sass' ] }, { test: /\\.(woff2?|ttf|eot|svg)$/, loader: 'url', query: { limit: 10000 } } 
  • Created src/style.scss with one line: @import 'bootstrap'; 用一行创建了src / style.scss:@ import @import 'bootstrap';

  • Added this line to the top of src/main.js: import './style.scss' 将此行添加到src / main.js的顶部: import './style.scss'

When I now run npm run dev I get the following error: 当我现在运行npm run dev我收到以下错误:

ERROR in ./src/main.js
Module not found: Error: Cannot resolve module 'style' in  Users/rstuart/Workspace/javascript/kapiche-demo/src
@ ./src/main.js 3:0-25

I'm not sure why this isn't working. 我不确定为什么这不起作用。

Also, related to this question, how do I get access to Bootstrap SASS variables inside my Vue components? 此外,与此问题相关,如何访问Vue组件中的Bootstrap SASS变量? If I understand what is going on here, the SASS will be compiled to CSS before being included inline in main.js meaning there is no access to any Bootstrap variables in my components. 如果我理解这里发生了什么,SASS将被编译为CSS,然后被包含在main.js内联中,这意味着我的组件中无法访问任何Bootstrap变量。 Is there a way to achieve this? 有没有办法实现这个目标?

I managed to solve this problem myself. 我自己设法解决了这个问题。 Instead of trying to directly import style.scss , I deleted the file entirely and I replaced the <style> element of App.vue with the following: 我没有尝试直接导入style.scss ,而是完全删除了该文件,并用以下内容替换了App.vue<style>元素:

  <style lang="sass">
    $icon-font-path: "../node_modules/bootstrap-sass/assets/fonts/bootstrap/";
    @import '../node_modules/bootstrap-sass/assets/stylesheets/_bootstrap';

    .wrapper {
      margin-top: $navbar-height;
    }
  </style>

This has the added bonus of making Bootstrap variables available in the style block of Vue components. 这还有在Vue组件的样式块中提供Bootstrap变量的额外好处。 Also, I removed { test: /\\.scss$/, loaders: [ 'style', 'css', 'sass' ] } from webpacker.base.conf.js entirely but kept the bit dealing with fonts. 另外,我从webpacker.base.conf.js完全删除了{ test: /\\.scss$/, loaders: [ 'style', 'css', 'sass' ] } ,但保留了处理字体的位。 The loader for .vue files already deals with sass. .vue文件的加载程序已经处理了sass。

I managed to worked it the right way like this: 我设法以正确的方式工作:

Vue: VUE:

<style lang="sass">
    $icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
    @import "~bootstrap-sass/assets/stylesheets/bootstrap";
</style>

webpack config loader: webpack配置加载器:

    {
      test: /\.(png|jpg|gif|svg)$/,
      loader: 'file-loader',
      options: {
        name: '[name].[ext]?[hash]'
      }
    },
    { 
      test: /\.scss$/, 
      loaders: [ 'style-loader', 'css-loader', 'sass-loader' ] 
    },
    { 
      test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
      loader: "url-loader?limit=10000&minetype=application/font-woff" 
    },
    { 
      test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
      loader: "file-loader" 
    }

Take note that I use the bootstrap-sass and not the default boostrap 请注意,我使用bootstrap-sass而不是默认的boostrap

It's trying to resolve the style module that you specified as a loader in this section of your webpack.base.conf.js: 它正在尝试解析您在webpack.base.conf.js的此部分中指定为加载器的style模块:

{ test: /\\.scss$/, loaders: [ **'style'**, 'css', 'sass' ] }

Given that you have a css and sass loader, that's likely an erroneous entry that you can remove to get yourself going. 鉴于你有一个css和sass加载器,这可能是一个错误的条目,你可以删除,让自己去。

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

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