React 中的前端路由 react-router-dom
程序员文章站
2022-07-02 09:33:38
安装react路由 npm install react-router-dom --save 准备好两个组件页面 Counter.js和myBtn.js,作为演示使用 修改index.js import React from 'react'; import ReactDOM from 'react-d ......
安装react路由
npm install react-router-dom --save
准备好两个组件页面 counter.js和mybtn.js,作为演示使用
修改index.js
import react from 'react'; import reactdom from 'react-dom'; import './index.css'; import * as serviceworker from './serviceworker'; import 'antd/dist/antd.css'; // or 'antd/dist/antd.less' import {browserrouter,route,link} from 'react-router-dom'; import counter from './counter'; import buttonsize from './mybtn'; /* <app />是jsx语法,需要react的支持 */ reactdom.render( <browserrouter> <div> <route path="/counter" component={counter} /> <route path="/mybtn" component={buttonsize} /> </div> </browserrouter>, document.getelementbyid('root') ); // if you want your app to work offline and load faster, you can change // unregister() to register() below. note this comes with some pitfalls. // learn more about service workers: https://bit.ly/cra-pwa serviceworker.unregister();
输入网址即可定位到指定页面
在某个页面使用link组件可以定义链接
mybtn.js
import react,{component} from 'react'; import { button, radio } from 'antd'; import { downloadoutlined } from '@ant-design/icons'; import {link} from 'react-router-dom'; class buttonsize extends react.component { state = { size: 'large', }; handlesizechange = e => { this.setstate({ size: e.target.value }); }; render() { const { size } = this.state; return ( <div> <radio.group value={size} onchange={this.handlesizechange}> <radio.button value="large">large</radio.button> <radio.button value="default">default</radio.button> <radio.button value="small">small</radio.button> </radio.group> <br /> <br /> <button type="primary" size={size}> primary </button> <button size={size}>default</button> <button type="dashed" size={size}> dashed </button> <br /> <link to="/counter"> <button type="link" size={size}> return last page </button> </link> <br /> <button type="primary" icon={<downloadoutlined />} size={size} /> <button type="primary" shape="circle" icon={<downloadoutlined />} size={size} /> <button type="primary" shape="round" icon={<downloadoutlined />} size={size} /> <button type="primary" shape="round" icon={<downloadoutlined />} size={size}> download </button> <button type="primary" icon={<downloadoutlined />} size={size}> download </button> </div> ); } } export default buttonsize;
点击返回列表页
关于路由的参数
修改index.js
修改mybtn.js
在counter.js中接收参数
上一篇: HTTP 请求报头详解
下一篇: VB.NET多线程入门