将CSS文件从React index.html链接到Flask后端

时间:2019-07-15 22:10:53

标签: css reactjs flask

我正在尝试将与Flask一起提供的一些静态后端内容的CSS模板链接到我的React前端。

这是我的结构:

client/
      src/
         components/
                   menu.jsx
      public/
            index.html
web/
   project/
          api/
             routes/
                   templates/
                            menu.html
                            loading.html
                   static/
                         css/
                            bootstrap.css
                            mug.css

在我的第一次尝试中,我试图像这样链接静态内容:

index.html

DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='css/bootstrap.css')}}">
    <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='css/mug.css')}}">    
    <title>My App</title>
  </head>
  <body>    
    <div id="root">
    </div>    
  </body>
</html>

以上操作无效。


我知道,如果我将所有CSS都放入“客户端/公共”中,则bootstrap.css可以工作,但是mug.css不能工作,因为当'menu.jsx '通过'dangerouslysetHTML'将我的后端模板整体呈现,如下所示:

menu.jsx

render(){
    var menu_html = this.state.menu_template;
    var menu_template = { __html: menu_html };
    return (
        <div id="parent">
            <h1 className="title is-1"><font color="#C86428">Menu</font></h1>
            <hr/><br/>
            <div dangerouslySetInnerHTML={menu_template}/>
        </div>
    );       
}

等等:

menus.html

<a href="{{ url_for('loading') }}</a>

等等:

loading.html

<div class="cup">
  <div class="coffee"></div>
</div>
<div class="smoke"></div>
<p class="txt">Brewing...<br><small></small></p>

最后:

mug.css

@import url(https://fonts.googleapis.com/css?family=Josefin+Sans:100,400);

body {
  background:rgb(223,189,150);
  font-family: 'Josefin Sans', sans-serif;
  text-align:center;
}

.cup {
  width:76px;
  height:76px;
  background:#dedede;
  border-radius:50%;
  position:absolute;
  left:50%;
  margin-left:-38px;
  top:100px;
  box-shadow:-4px -1px 0 rgba(0,0,0,0.2);
}

.cup:after{
  content:"";
  width:66px;
  height:66px;
  position:absolute;
  border-radius:50%;
  background:linear-gradient(-45deg, #cbcbcb, #f2f2f2 );
  left:50%;
  margin-left:-33px;
  top:5px;
}

.coffee{
  width:47px;
  height:47px;
  background:#613317;
  /*background-image: linear-gradient(15deg, transparent 0px, transparent 29px, rgba(255,255,255,0.05) 29px, rgba(255,255,255,0.05) 47px);*/
  border-radius:50%;
  position:absolute;
  left:50%;
  margin-left:-25px;
  z-index:1;
  top:50%;
  margin-top:-26px;
  border:3px solid #e9e9e9;
  box-shadow:-3px 2px 0 rgba(0,0,0,0.1);
  transform:rotate(30deg);
  transition:transform 0.3s;
}

.coffee:after{ /*assa*/
 content:"";
  width:15px;
  height:8px;
  position:absolute;
  left:-19px;
  top:18px;
  background:linear-gradient(90deg, #d6d6d6, #aeaeae);
  box-shadow:0px 2px 0 rgba(0,0,0,0.2);
}

.coffee:before {
  content:"";
  width:47px;
  height:47px;
  background-image: radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 95%), radial-gradient(5% 9%, ellipse, rgba(255,255,255,0.1), transparent 5%), radial-gradient(0% 0%, ellipse, rgba(255,255,255,0.3), transparent 55%);
  background-image: -webkit-radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 95%), -webkit-radial-gradient(5% 9%, ellipse, rgba(255,255,255,0.1), transparent 5%), -webkit-radial-gradient(0% 0%, ellipse, rgba(255,255,255,0.3), transparent 55%);
  background-image: -moz-radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 95%), -moz-radial-gradient(5% 9%, ellipse, rgba(255,255,255,0.1), transparent 5%), -moz-radial-gradient(0% 0%, ellipse, rgba(255,255,255,0.3), transparent 55%);
  position:absolute;
  left:0;
  border-radius:50%;
}
.cup:hover .coffee {
  transform:rotate(15deg);
}

.smoke {
  width:70px;
  height:100px; 
  background:;
  position:absolute;
  left:50%;
  margin-left:-40px;
  top:43px;
  background-image: -webkit-radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 25%), -webkit-radial-gradient(25% 70%, ellipse, rgba(255,255,255,0.2), transparent 15%), -webkit-radial-gradient(35% 61%, ellipse, rgba(255,255,255,0.2), transparent 10%), -webkit-radial-gradient(55% 35%, ellipse, rgba(255,255,255,0.2), transparent 15%);
  background-image: -moz-radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 25%), -moz-radial-gradient(25% 70%, ellipse, rgba(255,255,255,0.2), transparent 15%), -moz-radial-gradient(35% 61%, ellipse, rgba(255,255,255,0.2), transparent 10%), -moz-radial-gradient(55% 35%, ellipse, rgba(255,255,255,0.2), transparent 15%);
  background-image: radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 25%), radial-gradient(25% 70%, ellipse, rgba(255,255,255,0.2), transparent 15%), radial-gradient(35% 61%, ellipse, rgba(255,255,255,0.2), transparent 10%), radial-gradient(55% 35%, ellipse, rgba(255,255,255,0.2), transparent 15%);
  animation:bk 11s infinite ;
}

.smoke:after {
  content:"";
  width:100px;
  height:100px; 
  background:;
  position:absolute;
  background-image: -webkit-radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 25%), -webkit-radial-gradient(25% 70%, ellipse, rgba(255,255,255,0.2), transparent 15%), -webkit-radial-gradient(35% 61%, ellipse, rgba(255,255,255,0.2), transparent 10%);
  background-image: -moz-radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 25%), -moz-radial-gradient(25% 70%, ellipse, rgba(255,255,255,0.2), transparent 15%), -moz-radial-gradient(35% 61%, ellipse, rgba(255,255,255,0.2), transparent 10%);
  background-image: radial-gradient(75% 59%, ellipse, rgba(255,255,255,0.2), transparent 25%), radial-gradient(25% 70%, ellipse, rgba(255,255,255,0.2), transparent 15%), radial-gradient(35% 61%, ellipse, rgba(255,255,255,0.2), transparent 10%);
  animation:bk 2s 1s infinite;
}


@keyframes bk {
  0%{ background-position:0 0; opacity:0; }
  15%, 85% { opacity:1;}
  100%{ background-position:-20px -25px, -5px -25px, 0px -25px, 0px -25px; opacity:0;}
}

.txt{
  position:absolute;
  top:184px;
  width:150px;
  left:50%;
  margin-left:-75px;
}

.txt a{
  text-decoration:none;
  color:#613317;
}

.txt a:hover {
  text-decoration:underline;
}

问题:

那么,如何将此后端CSS链接到前端index.html,否则如何将其应用?

1 个答案:

答案 0 :(得分:0)

我不知道(更像是我不认为),有一种方法可以使用正常的Jinja模板将Flask与react直接链接。如果要使用像ReactJS这样的前端框架,则必须使用两个完全独立的系统。

处理此问题的最佳方法通常是使用API​​。

使用要生成的API端点创建后端。这可以通过Flask Restful完成。 API端点基本上将以JSON格式生成数据。

API端点可由前端React JS使用。我没有相应的链接,但是我敢肯定有很多在线资源。

现在,如果您需要在ReactJS端更改特定的CSS项,则可以发送特定的标志来指示需要更改的内容,并且可以处理逻辑。例如,您可以使用以下字段从后端生成端点:

{
    "user-team": "blue" 
}

在ReactJS上,可以进行逻辑检查该特定字段并基于该字段更改特定元素的颜色。