0

文件夹结构 我如何在反应中使用 css。不断抛出 ./src/index.js Module not found: Can't resolve './app.css' in 'c:\Repo\ecom-front\src'。我知道我做错了什么,所以需要一些帮助。 ecom-front/src/core/Menu.js

import React from "react";
import {Link, withRouter} from "react-router-dom";
import styles from "./app.css";

const Menu = () => (
 <div>
  <nav class="navbar">
       <span class="navbar-toggle" id="js-navbar-toggle">
           <i class="fas fa-bars"></i>
       </span>
       <a href="#" class="logo">logo</a>
       <ul class="main-nav" id="js-menu">
           <li>
               <a href="#" class="nav-links">Home</a>
           </li>
           <li>
               <a href="#" class="nav-links">Products</a>
           </li>
           <li>
               <a href="#" class="nav-links">About Us</a>
           </li>
           <li>
               <a href="#" class="nav-links">Contact Us</a>
           </li>
           <li>
               <a href="#" class="nav-links">Blog</a>
           </li>
       </ul>
   </nav>
  </div>
);

export default withRouter(Menu);

ecom-front/public/index.html

<head>
    <link rel="stylesheet" type="text/css" href="app.css">
    // other code......
  </head>

ecom-front/src/user/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Routes from './Routes';
import './app.css';

ecom-front/src/core/app.css

{
            box-sizing: border-box;
            padding: 0;
            margin: 0;
        }

        body {
            font-family: 'Josefin Sans', sans-serif;
        }

        .navbar {
            font-size: 18px;
            background-image: linear-gradient(260deg, #2376ae 0%, #c16ecf 100%);
            border: 1px solid rgba(0, 0, 0, 0.2);
            padding-bottom: 10px;
        }

        .main-nav {
            list-style-type: none;
            display: none;
        }

        .nav-links,
        .logo {
            text-decoration: none;
            color: rgba(255, 255, 255, 0.7);
        }

        .main-nav li {
            text-align: center;
            margin: 15px auto;
        }

        .logo {
            display: inline-block;
            font-size: 22px;
            margin-top: 10px;
            margin-left: 20px;
        }

        .navbar-toggle {
            position: absolute;
            top: 10px;
            right: 20px;
            cursor: pointer;
            color: rgba(255, 255, 255, 0.8);
            font-size: 24px;
        }

        .active {
            display: block;
        }

        @media screen and (min-width: 768px) {
            .navbar {
                display: flex;
                justify-content: space-between;
                padding-bottom: 0;
                height: 70px;
                align-items: center;
            }

            .main-nav {
                display: flex;
                margin-right: 30px;
                flex-direction: row;
                justify-content: flex-end;
            }

            .main-nav li {
                margin: 0;
            }

            .nav-links {
                margin-left: 40px;
            }

            .logo {
                margin-top: 0;
            }

            .navbar-toggle {
                display: none;
            }

            .logo:hover,
            .nav-links:hover {
                color: rgba(255, 255, 255, 1);
            }
        }

所以我尝试了很多不同的方法来放置外部 css 文件,因为我正在尝试使用 flexbox 或者不依赖于引导程序。我不知道我是否应该安装一个 npm 包,或者我是否遗漏了一些东西,因为我只知道实现 css 的传统方法,而不是使用 REACT。

4

2 回答 2

2

您错误地导入了 css 文件。

代替

import styles from "./app.css";

做这个

import "./app.css";

没有从 css 文件导出的样式。并在课堂上使用 className

<div className="class">Get work done in over 1450 different categories</div> 

编辑:这是因为您从中导入 css 的路径ecom-front/src/index.js是错误的

它应该是

import './core/app.css';
于 2019-11-05T10:54:24.580 回答
1

您无需在该部分中链接它。

此外,使用“className”(区分大小写)代替“class”,如果要应用“some_class”类,请使用语法

className={ styles.some_class }
于 2019-11-05T10:47:43.913 回答