简体   繁体   English

如何从样式组件中的另一个文件继承元素的 styles

[英]How to inherit styles of an element from another file in styled-components

I have a navigation bar on the home page with background-color: white;我在主页上有一个导航栏, background-color: white; On moving to another page.在移动到另一个页面。 I want it to turn black .我想让它black

My Navigation Styles file:我的导航 Styles 文件:

import styled from 'styled-components'

export const Nav = styled.nav`
 background-color: #fff;
`

My Navigation file where I am importing the styles:我在其中导入 styles 的导航文件:

import { Link } from 'react-router-dom'
import Nav from './NavStyles'

const NavBar = () => {
    return(
        <Nav>     
            <ul>
                <li>
                    <Link to="/">Home</Link>
                </li>
                <li>
                    <Link to="/shop">Shop</Link>
                </li>
                <li>
                    <Link to="/cart">Cart</Link>
                </li>
            </ul>
        </Nav>
    )
}

export default NavBar

My ShopStyles file (where changing the background-color):我的 ShopStyles 文件(更改背景颜色的地方):

import styled from 'styled-components'
import  { Nav } from '../Nav/NavStyles'

const ShopNav = styled(Nav)`
    background-color: #323232;
`;

export default ShopNav

My Shop file:我的商店文件:

import ShopNav from './ShopStyles'
import { Nav } from '../Nav/NavStyles'

const Shop = () => {
    return(
        <div>
            <Nav>
               <ShopNav></ShopNav>
            </Nav>
            
            <h1>Over here my shop design will come</h1>
        </div>
    )
}

export default Shop

App.js:应用程序.js:

import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'
import Home from './components/Home/Home'
import NavBar from './components/Nav/Nav'
import Shop from './components/Shop/Shop'

const App = () => {
  return (
    <Router>
      <NavBar />
      <Switch>
        <Route exact path="/"> <Home /> </Route>
        <Route exact path='/shop'> <Shop /> </Route>
      </Switch>
    </Router>
    
  );
}

export default App;

How do I get the color black with my navigation bar?如何使用导航栏获得黑色?

I don't understand about your worry.我不明白你的担心。 The code which you've written is correct and I have also tested your code.你写的代码是正确的,我也测试了你的代码。 I recommend that you will check your code again.我建议您再次检查您的代码。

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

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