[英]React js - make button link to a different app
我想制作一个按钮来链接到带有反应库的不同应用程序。 我的代码看起来像这样
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 并添加到之前的 url 中,例如 ( http://localhost:3000/https://github.com ) 并且不会在任何地方使用 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"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.