react 工程实现按钮点击打开一个新的页签

198 阅读1分钟

​​ ​

import React, { PureComponent } from 'react';

export default class lt extends PureComponent {

state = {
  
   host: '',

}

 lzp= () => {

    const w = window.open('about:blank');
    const { host } = this.state;
    // 要打开的新页面的url
    w.location.href=`http://localhost:${host}/#/lt/lzp`;
  };

// 页面加载完执行
  componentDidMount() {

    // 获取当前计算机的端口号
    this.setState({
      host: window.location.host.split(':')[1],
    });
  
  render(){

    return(
      <div>
        
         <Button onClick={this.lzp}>按钮<Button>
        
      </div>
    )
  }
}