简体   繁体   English

React.js / Next.js - 如何从一篇文章链接到另一篇文章?

[英]React.js / Next.js - how to link from one article to another?

Suppose this example with two articles (full example here https://github.com/codebushi/nextjs-starter-dimension/blob/master/components/Main.js )假设这个例子有两篇文章(完整的例子在这里https://github.com/codebushi/nextjs-starter-dimension/blob/master/components/Main.js

import PropTypes from 'prop-types';

class Main extends React.Component {
  render() {

    let close = <div className="close" onClick={() => {this.props.onCloseArticle()}}></div>

    return (
      <div id="main" style={this.props.timeout ? {display: 'flex'} : {display: 'none'}}>

        <article id="intro" className={`${this.props.article === 'intro' ? 'active' : ''} ${this.props.articleTimeout ? 'timeout' : ''}`} style={{display:'none'}}>
          <h2 className="major">Intro</h2>
          <span className="image main"><img src="/static/images/pic01.jpg" alt="" /></span>
          <p>Nam maximus erat id euismod egestas. By the way, check out my <a href="#work">awesome work</a>.</p>
          {close}
        </article>

        <article id="work" className={`${this.props.article === 'work' ? 'active' : ''} ${this.props.articleTimeout ? 'timeout' : ''}`} style={{display:'none'}}>
          <h2 className="major">Work</h2>
          <span className="image main"><img src="/static/images/pic02.jpg" alt="" /></span>
          <p>Adipiscing magna sed dolor elit. Praesent eleifend dignissim arcu, at eleifend sapien imperdiet ac. Aliquam erat volutpat. Praesent urna nisi, fringila lorem et vehicula lacinia quam. Integer sollicitudin mauris nec lorem luctus ultrices.</p>
          {close}
        </article>

      </div>
    )
  }
}

Main.propTypes = {
  route: PropTypes.object,
  article: PropTypes.string,
  articleTimeout: PropTypes.bool,
  onCloseArticle: PropTypes.func,
  timeout: PropTypes.bool
}

export default Main

How do I create a link in the intro article to open the work article?如何在intro文章中创建链接以打开work文章? In the example there's an <a href='#work'> , but when I click it there's no action at all.在示例中有一个<a href='#work'> ,但是当我单击它时,根本没有任何动作。

You can do it by importing 'Link' from Next.js:您可以通过从 Next.js 导入“链接”来实现:

import Link from "next/link";

Then you can add:然后你可以添加:

    <Link href="work">
          <a>Go To Work!</a>
    </Link>

Full documentation from Next.js: https://nextjs.org/docs/api-reference/next/link Next.js 的完整文档: https://nextjs.org/docs/api-reference/next/link

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

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