ReactJS 链接标签 - 未捕获的引用错误:链接未定义

问题描述 投票:0回答:4

如何正确添加

<a>
标签?添加
<Link></Link>

时出现此错误
Uncaught ReferenceError: Link is not defined

代码:

   render() {
        return (
            <div>
                <Link className="button-close-overlay"><span className="glyphicon glyphicon-remove"></span></Link>
                <article>
                    <div className="container">
                        <div className="content align-center" dangerouslySetInnerHTML={{__html: this.state.article.content}}></div>
                    </div>
                </article>
            </div>
        );
    }

完整代码:

class Article extends React.Component
{
    constructor(props) {
        super(props);
        this.state = {
            article: [],
        };
    }

    // Then fetch the data using $.get():
    componentDidMount() {
        this.serverRequest = $.get(this.props.source, function (result) {
            this.setState({
                article: result
            });
        }.bind(this));
    }

    componentWillUnmount() {
        this.serverRequest.abort();
    }

    render() {
        return (
            <div>
                <link className="button-close-overlay"><span className="glyphicon glyphicon-remove"></span></link>
                <article>
                    <div className="container">
                        <div className="content align-center" dangerouslySetInnerHTML={{__html: this.state.article.content}}></div>
                    </div>
                </article>
            </div>
        );
    }
}

export { Article as default }

有什么想法吗?

reactjs ecmascript-6
4个回答
22
投票

如果您正在考虑 React 路由器

Link
。然后你需要导入它。

import { Link } from 'react-router';

var Link = require('react-router').Link

否则渲染一个纯粹的

<a>
就可以了!


19
投票

另一种选择是

import {Link} from 'react-router-dom';

2
投票

如果你使用Nextjs,你也可以使用Link from next/link

import Link from "next/link"

0
投票

有时当您没有在同一页面中导入 React 时会出现此错误

从“react-router-dom”导入{链接}; 从“反应”导入反应;

© www.soinside.com 2019 - 2024. All rights reserved.