繁体   English   中英

通过字符串返回函数检索HTML元素的href链接?

[英]Retrieving href link for HTML element via String returning function?

我正在使用React.js,并且尝试在我的渲染函数中检索以下html元素的地址链接:

<a class="form-label" href={this.link.bind(this)}> Link </a>

功能链接如下:

link() { 
return "192.168.3.9:8080/cars/" + this.state.carType; 
}

其中this.state.carType存储以字符串this.state.carType存储的汽车的类型。

但是,这是我的链接:

本地主机:8080 / function%20()%20%7B%20 [native%20code]%20%7D

有任何想法吗?

谢谢。

一种简单的方法是添加ReactRedirect并相应地使用

按照链接ReactRedirect

link() { 
return (
   <ReactRedirect location={`192.168.3.9:8080/cars/${this.state.carType}`}>
  </ReactRedirect>
 );
}

我认为您需要在构造函数中绑定链接函数,如本例所示,它可以工作。

constructor() {
   super();

   this.state = {
     carModel: "325i"
   };

   this.getLinkUrl = this.getLinkUrl.bind(this);
}
getLinkUrl() {
  return "http://192.168.3.9:8080/cars/" + this.state.carModel;
}

测试链接

暂无
暂无

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

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