在react.js中添加锚点标签参数

4

我正在尝试将用户名添加到我的锚点标签的href属性中。 我的代码如下:

var Name = React.createClass({

    render: function() {
        return (
            <div>
                <a href="www.example.com/"+ { this.props.username} +"" className="post-title">{this.props.name}</a><br />
            </div>
        );
    }

});

但是,我的程序出现了一个错误,显示在 href 属性附近有未定义的标记 "+"。我认为这是因为我试图在渲染方法中添加它所导致的。那么,是否有其他方法可以解决这个问题呢?

谢谢。

2个回答

9

去掉+号。正确的写法应该是:

<a href={"www.example.com/" + this.props.username} ...

我已经尝试过了。然后链接重定向到http://www.example.com/{this.props.username}而不是http://www.example.com/anyusername - Swapnil Bhikule

2
另一种方法是:

<a href={`https://www.example.com/${this.props.username1}`}>link</a>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接