样式和嵌套组件

现在我们来添加一个简单的更改到我们的主页。 我们已经隔离了我们的链接组件,如下所示:

  1. import Layout from '../components/MyLayout.js'
  2. import Link from 'next/link'
  3. function getPosts () {
  4. return [
  5. { id: 'hello-nextjs', title: 'Hello Next.js'},
  6. { id: 'learn-nextjs', title: 'Learn Next.js is awesome'},
  7. { id: 'deploy-nextjs', title: 'Deploy apps with ZEIT'},
  8. ]
  9. }
  10. const PostLink = ({ post }) => (
  11. <li>
  12. <Link as={`/p/${post.id}`} href={`/post?title=${post.title}`}>
  13. <a>{post.title}</a>
  14. </Link>
  15. </li>
  16. )
  17. export default () => (
  18. <Layout>
  19. <h1>My Blog</h1>
  20. <ul>
  21. {getPosts().map((post) => (
  22. <PostLink key={post.id} post={post}/>
  23. ))}
  24. </ul>
  25. <style jsx>{`
  26. h1, a {
  27. font-family: "Arial";
  28. }
  29. ul {
  30. padding: 0;
  31. }
  32. li {
  33. list-style: none;
  34. margin: 5px 0;
  35. }
  36. a {
  37. text-decoration: none;
  38. color: blue;
  39. }
  40. a:hover {
  41. opacity: 0.6;
  42. }
  43. `}</style>
  44. </Layout>
  45. )

pages/index.js 中替换上述内容

会发生什么?

  1. ✦没有变化
  2. ✓存在h1的类型,但不适用于链接
  3. ✦页面上有错误
  4. ✦控制台上有错误