javascript - 在 ReactJS 应用程序中加载全局 Bootstrap 配置

标签 javascript css twitter-bootstrap reactjs

我正在使用 react-universally作为 ReactJS 应用程序样板和用于样式化的 bootstrap 4。这是主要组件 DemoApp:

import 'normalize.css/normalize.css';

import React from 'react';
import Switch from 'react-router-dom/Switch';
import Route from 'react-router-dom/Route';
import Helmet from 'react-helmet';

import config from '../../utils/config';

import './globals.css';

import Error404 from './Error404';
import Header from './Header';

import AsyncHomeRoute from './AsyncHomeRoute';
import AsyncCounterRoute from './AsyncCounterRoute';
import AsyncAboutRoute from './AsyncAboutRoute';

/// Should change the language below according to the user preference language

function DemoApp() {
  return (
    <div style={{ padding: '2rem' }}>
      <Helmet>
        <html lang="en" />
        <title>{config('htmlPage.defaultTitle')}</title>
        <meta name="application-name" content={config('htmlPage.defaultTitle')} />
        <meta name="description" content={config('htmlPage.description')} />
        <meta charSet="utf-8" />
        <meta httpEquiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="msapplication-TileColor" content="#2b2b2b" />
        <meta name="msapplication-TileImage" content="/favicons/mstile-144x144.png" />
        <meta name="theme-color" content="#2b2b2b" />
        {/*
          A great reference for favicons:
          https://github.com/audreyr/favicon-cheat-sheet
          It's a pain to manage/generate them. I run both these in order,
          and combine their results:
          http://realfavicongenerator.net/
          http://www.favicomatic.com/
        */}
        <link
          rel="apple-touch-icon-precomposed"
          sizes="152x152"
          href="/favicons/apple-touch-icon-152x152.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="144x144"
          href="/favicons/apple-touch-icon-144x144.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="120x120"
          href="/favicons/apple-touch-icon-120x120.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="114x114"
          href="/favicons/apple-touch-icon-114x114.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="76x76"
          href="/favicons/apple-touch-icon-76x76.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="72x72"
          href="/favicons/apple-touch-icon-72x72.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="57x57"
          href="/favicons/apple-touch-icon-57x57.png"
        />
        <link
          rel="apple-touch-icon-precomposed"
          sizes="60x60"
          href="/favicons/apple-touch-icon-60x60.png"
        />
        <link
          rel="apple-touch-icon"
          sizes="180x180"
          href="/favicons/apple-touch-icon-180x180.png"
        />
        <link rel="mask-icon" href="/favicons/safari-pinned-tab.svg" color="#00a9d9" />
        <link rel="icon" type="image/png" href="/favicons/favicon-196x196.png" sizes="196x196" />
        <link rel="icon" type="image/png" href="/favicons/favicon-128.png" sizes="128x128" />
        <link rel="icon" type="image/png" href="/favicons/favicon-96x96.png" sizes="96x96" />
        <link rel="icon" type="image/png" href="/favicons/favicon-32x32.png" sizes="32x32" />
        <link rel="icon" sizes="16x16 32x32" href="/favicon.ico" />
        <meta name="msapplication-TileColor" content="#2b2b2b" />
        <meta name="msapplication-TileImage" content="/favicons/mstile-144x144.png" />
        <meta name="msapplication-square70x70logo" content="/favicons/mstile-70x70.png" />
        <meta name="msapplication-square150x150logo" content="/favicons/mstile-150x150.png" />
        <meta name="msapplication-wide310x150logo" content="/favicons/mstile-310x150.png" />
        <meta name="msapplication-square310x310logo" content="/favicons/mstile-310x310.png" />
        <link rel="manifest" href="/manifest.json" />

        {/*
          NOTE: This is simply for quick and easy styling on the demo. Remove
          this and the related items from the Content Security Policy in the
          global config if you have no intention of using milligram.
        */}
        <link
          rel="stylesheet"
          href="https://fonts.googleapis.com/css?family=Tangerine"
        />
        <link
          rel="stylesheet"
          href="/scripts/css/bootstrap.css"
        />
      </Helmet>
      <Header />
      <div style={{ paddingTop: '2rem', paddingBottom: '2rem' }}>
        <Switch>
          <Route exact path="/" component={AsyncHomeRoute} />
          <Route path="/counter" component={AsyncCounterRoute} />
          <Route path="/about" component={AsyncAboutRoute} />
          <Route component={Error404} />
        </Switch>
      </div>
    </div>
  );
}

export default DemoApp;

还有我的 global.css 文件,它设置了所有应用程序的样式:

   html {
      box-sizing: border-box;
      font-family: 'Tangerine', serif !important;
      color: red;
    }

    *,
    *:before,
    *:after {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    /* Inline our ul for our menu */
    ul {
      margin: 0;
      padding: 0;
      list-style-type: none;
    }

    ul li { display: inline; margin: 0 .5rem; }

我的问题是字体集 (Tangerine) 没有显示。它正在被应用,但随后被 boostraps _reboot.css 覆盖,将所有字体设置为 bootstrap 默认值。

我可以想象这是由于 import "./globals.css" 在加载 bootstrap 之前执行的命令,所以当我加载 bootstrap 时它调用 _reboot 重置所有参数,包括其字体,覆盖我的设置。

我如何使用 DemoApp 显示的组件方法,在 Bootstrap 完成其设置周期后反转该顺序并加载 globals.css,以便我的字体可以毫无问题地使用?

如果不可能,是否有其他解决方案可以在 ReactJS 环境中全局设置 Bootstrap 变量?

感谢您的帮助。

最佳答案

globals.css应该覆盖全局 bootstrap.css或者至少在 bootstrap.css 之后应用已加载。

由于文件是全局的,您可以在 Bootstrap 后创建一个链接:

<link
  rel="stylesheet"
  href="/scripts/css/bootstrap.css"
/>
<link
  rel="stylesheet"
  href="/scripts/css/globals.css"
/>

我错过了什么吗?

关于javascript - 在 ReactJS 应用程序中加载全局 Bootstrap 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43502980/

相关文章:

javascript - Ramda.js 如何用挖掘对象进行采摘

javascript - 任何人都可以指导我使用 js 或 python 组合可视化吗?

twitter-bootstrap - 初始化后设置选项时,datetimepicker 中的 disabledTimeIntervals 不起作用

html - safari 和 chrome 之间点击时导航栏品牌颜色不同

javascript - 调用函数在上次调用中销毁回调

css - wordpress中的中心导航

JavaScript 切换按钮

javascript - "next"和 "previous"按钮,带有 div 的附加内容

javascript - 使用 bootstrap-modal modalOverflow 检测滚动事件?

javascript - 登录表单未呈现