简体   繁体   English

我想使用 CSS 样式设置 JS 文件的样式,但出现错误

[英]I would like to style JS file in react using CSS styling but I'm getting an error

import React from 'react'
import './myStyle.css'
function Stylesheet(){
    return(
        <div>
            <h1>Stylesheet</h1>
        </div>
    )
}
export default Stylesheet

This I'm getting an error这我得到一个错误

./src/Components/stylesheet.js Module not found: Can't resolve './myStyle.css' in 'D:\Documents\WebProject\React\hello-world\src\Components' ./src/Components/stylesheet.js 未找到模块:无法解析 'D:\Documents\WebProject\React\hello-world\src\Components' 中的 './myStyle.css'

There is no need to create a component for styling a component.无需为组件样式创建组件。 Just write your css style and import it like import './myStyle.css'只需编写您的 css 样式并像import './myStyle.css'一样导入它

There are different options to style React components.I will explain few options here. React 组件的样式有不同的选项。我将在这里解释几个选项。

CSS Stylesheet: CSS 样式表:

Add a Stylesheet.css file as below.添加一个Stylesheet.css文件,如下所示。

.boxColor {
  background-color: red;
}
    

Add a component Stylesheet.js .添加一个组件Stylesheet.js Simply import css file import './Stylesheet.css'只需导入 css 文件import './Stylesheet.css'

import React from 'react';
import './Stylesheet.css';

const Stylesheet = () => (
  <div className="boxColor">
    <p>Get started with CSS styling</p>
  </div>
);

export default Stylesheet;

Inline styling : In React, inline styles are not specified as a string.内联样式:在 React 中,内联 styles 未指定为字符串。 Instead they are specified with an object.相反,它们使用 object 指定。

Add Stylesheet.js file.添加Stylesheet.js文件。

import React from 'react';

const boxColor = {
  background-color: red;
};
    
const Stylesheet = () => (
 <div style={boxColor}>
   <p>Get started with CSS styling</p>
 </div>
);
    
export default Stylesheet;
  • We can create a variable that stores style properties and then pass it to the element like style={boxColor}我们可以创建一个存储样式属性的变量,然后将其传递给style={boxColor}之类的元素
  • We can also pass the styling directly style={{background-color: 'red'}}我们也可以直接传递样式style={{background-color: 'red'}}

There few more ways like Styled-component and CSS Modules to style react component.还有一些像Styled-componentCSS Modules这样的方式来设置 react 组件的样式。

for example,you have two files, first is practice.js and second is style.css then import your style.css in practice.js as following.例如,您有两个文件,第一个是practice.js ,第二个是style.css然后在practice.js中导入您的style.css ,如下所示。

practice.js file练习.js 文件

import React from 'react';
import './style.css'; //importing style sheet

export default function practice() {
     return (
          <div>
               <h1 className="heading">Hellooooooo</h1>    
          </div>
     )
}

and your style.css file is你的 style.css 文件是

.heading{
     color: white;
     background-color:  black;
}

暂无
暂无

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

相关问题 我使用节点 js multer 遇到文件上传问题 - I'm getting file upload problem using node js multer 我的textarea文本没有使用javascript样式化到CSS - My textarea text isn't styling the way that i would like it to CSS with javascript 我正在尝试构建一个托盘应用程序并希望对其进行样式设置,需要帮助才能开始 - I'm trying to build a tray app and would like to style it, need help to get started 我尝试使用 javascript 访问我系统上的本地映像,但出现如下错误: - I tried to access a local image on my system using javascript but i'm getting error like : 我是 JS 新手,我想知道如何从 HTML 中获取特定信息 - I'm new to JS, I would like to know how I can get specific information from HTML 我是节点的新手。 js,我想知道我如何处理猫鼬咨询结果? - I'm a newbie with node. js, I would like to know how I handle a mongoose consultation result? 我正在尝试创建一个机器人,我希望我的所有消息都包含在 JS 中的同一个 div 中 - I'm trying to create a bot and I would like all my messages to be contained with in the same div in JS ExpressionEngine,我想将JS文件添加到模板中 - ExpressionEngine, I would like to add a JS file to a template 使用gulp-browserify为我的React.js模块我在浏览器中得到'require is not defined' - Using gulp-browserify for my React.js modules I'm getting 'require is not defined' in the browser 为什么在 React.js 中使用 useEffect 钩子时会收到这些警告? - Why I'm getting these warnings while using useEffect hook in React.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM