我想制作一个按钮,以react librery链接到另一个应用程序。我的代码看起来像这样
import React, { Component } from "react";
import { Link, NavLink, Redirect } from "react-router-dom";
import "./subportfolio.scss";
import Coffee1 from "../../assets/coffee-1.jpg";
import Coffee2 from "../../assets/coffee-2.jpg";
import Coffee3 from "../../assets/coffee-3.jpg";
import Coffee4 from "../../assets/coffee-4.jpg";
import CoffeeShop from "../../assets/coffee-shop-1.jpg";
import Tea1 from "../../assets/tea-1.jpg";
import Tea2 from "../../assets/tea-2.jpg";
import Tea3 from "../../assets/tea-3.jpg";
class SubPortfolio extends Component {
state = {
projectImg: {
project1: Coffee1,
project2: Coffee2,
project3: Coffee3,
project4: Coffee4,
project5: CoffeeShop,
project6: Tea1,
project7: Tea2,
project8: Tea3
},
direct: ""
};
directToGithub = () => {
this.props.history.go("https://github.com");
};
render() {
console.log(this.state.direct);
return (
<div className="subPortfolio">
<div className="subPortfolio__content">
<div className="subPortfolio__content--detail">
<h2>Image Format</h2>
<div className="subPortfolio__content--refer">
<div className="subPortfolio__content--button">
<button
id="github"
type="button"
onClick={this.directToGithub}
>
github
</button>
</div>
</div>
</div>
</div>
</div>
);
}
}
export default SubPortfolio;
我尝试了所有历史记录属性,但所有这些都引发错误
TypeError: Cannot read property 'go' of undefined
SubPortfolio.directToGithub
C:/Users/Ermais Kidane/Documents/REACT/portfolio/src/components/subportfolio/subportfolio.js:30
27 | };
28 |
29 | directToGithub = () => {
> 30 | this.props.history.go("https://github.com");
| ^ 31 | };
32 | render() {
33 | // const projectsImage = Object.keys(this.state.projectImg).map(proImg => {
我尝试使用Link React-router-dom,并使用(http://localhost:3000/https://github.com)之类的以前的URL进行添加,并且在任何地方都不使用make。
我如何链接到另一个网站?感谢您的帮助。
如果您要定向到外部网站,只需使用标准的<a></a>
标签
<a href="https://www.github.com"><button id="github" type="button">github</button></a>
如果您尝试使用<Link>
定向到内部链接,它将看起来更像这样
<Link to="/github"><button id="github" type="button">github</button></Link>
然后必须使用路由器在app.js中设置链接,然后将其定向到指定的组件
[如果要以编程方式定向到外部网站,也可以使用window.location = "https://github.com"
,或者使用全局window
对象,则使用location = "https://github.com"
。