【发布时间】:2019-07-21 21:05:43
【问题描述】:
请查看代码。我想有条件地为 about 链接添加一些额外的样式,所以我将它添加到标签的模板字符串的 ${} 中。这不会应用新样式。
我尝试过连接模板字符串。那也没用。
我无法在标签中设置 style={style},因为那样我将无法使用 :hover 样式。
import Link from 'next/link'
import { withRouter } from 'next/router'
const Header = ({ children, router, href }) => {
const isIndex = router.pathname === "/";
const isAbout = router.pathname === "/about";
return (
<div>
<Link href="/">
<a id="home">Timers</a>
</Link>
<Link href="/about">
<a id="about">About</a>
</Link>
<style jsx>{
`
a {
font-family: 'Montserrat Subrayada', sans-serif;
color: #ffffff;
text-decoration: none;
font-size: 24px;
padding: 2px;
margin-right: 30px;
}
a:hover {
color: #000000;
background-color: #ffffff;
border-radius: 2px;
}
${isAbout ? `
#about {
background-color: red;
color: #000000;
}
#about:hover {
background-color: blue;
color: #ffffff;
}
` : ``}
`
}</style>
</div>
)}
export default withRouter(Header)
如何应用条件样式?
【问题讨论】:
标签: javascript reactjs jsx next.js