简体   繁体   English

Bootstrap:按钮样式在 React/Rails/Webpacker 设置中未正确显示

[英]Bootstrap: button styles not showing properly in React/Rails/Webpacker setup

I have a Rails 6 project set up with Webpacker & React .我有一个使用WebpackerReact设置的Rails 6项目。 I installed Bootstap via yarn and is configure to work with webpacker.我通过纱线安装了Bootstap并配置为与 webpacker 一起使用。 Everything is seems to work except buttons background color comes in a shade of black.除了buttons背景颜色呈黑色外,一切似乎都有效。 I have spent hours trying to figure out why with no luck.我花了几个小时试图弄清楚为什么没有运气。 For example the following button will not render in primary color blue as it should.例如,下面的按钮不会像它应该的那样以原色蓝色呈现。

<button type="button" class="btn btn-primary">Primary</button> **do not render properly**

/packs/stylesheets/application.scss: /packs/stylesheets/application.scss:

`@import "~bootstrap/scss/bootstrap";`

/packs/application.js: /packs/application.js:

import 'bootstrap'
import './stylesheets/application'

config/webpack/environment.js looks like this: config/webpack/environment.js 看起来像这样:

const { environment } = require('@rails/webpacker');

const webpack = require('webpack')
environment.plugins.append('Provide',
  new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Popper: ['popper.js', 'default']
 })
);

environment.loaders.append("graphql", {
  test: /\.(graphql|gql)$/,
  exclude: /node_modules/,
  loader: 'graphql-tag/loader',
});

module.exports = environment;

Any help you could provide is greatly appreciated.非常感谢您可以提供的任何帮助。 Thank you in advance for your time.提前感谢您的时间。

Why are you using class attribute in the button-tag?为什么在按钮标签中使用class属性?

That should be className , right?那应该是className ,对吧?


Moreover, I'm using these configurations(seems similar to yours) and working flawlessly,此外,我正在使用这些配置(似乎与您的类似)并且工作完美,

  • Versions版本

    • "@rails/webpacker": "4.2.2" "@rails/webpacker": "4.2.2"
    • "bootstrap": "^4.4.1" “引导程序”:“^4.4.1”
    • Rails 6.0.1导轨 6.0.1
  • Project-structure for packs ,包的项目结构

    项目结构

  • Imports进口

    • application.sccs应用程序.sccs

       @import "~bootstrap/scss/bootstrap";
    • hello_react.jsx hello_react.jsx

       import './scss/application.scss';
  • hello_react.jsx holds the root component hello_react.jsx包含root组件

    document.addEventListener('DOMContentLoaded', () => { ReactDOM.render( <Provider store={store}> <App/> </Provider>, document.body.appendChild(document.createElement('div')), ) })
  • config/webpack/environment.js配置/webpack/environment.js

     const { environment } = require('@rails/webpacker') module.exports = environment
  • Demo演示

    <button type="button" className="btn btn-primary">Primary</button>

    在此处输入图片说明

Problem resolved!!问题解决!! It seems like the issue was related to Activeadmin , but not sure.该问题似乎与Activeadmin有关,但不确定。 But, after removing #= require_tree .但是,在删除#= require_tree . from assets/stylesheets/application.scss , it worked.assets/stylesheets/application.scss ,它起作用了。

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

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