React 中的前端路由 react-router-dom
程序员文章站
2022-04-14 16:11:42
安装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中接收参数
上一篇: identityserver4源码解析_3_认证接口
下一篇: Spring通过名称获取Bean示例
推荐阅读
-
前端框架学习总结之Angular、React与Vue的比较详解
-
C#调用Activex中串口电子秤的数据,并将电子秤的数据显示到前端页面
-
在Create React App中启用Sass和Less的方法示例
-
深入理解react-router 路由的实现原理
-
关于React的一些小知识-拖泥的前端之路-SegmentFault思否
-
示例React-router 路由切换动画的实现
-
JavaScript中的浅拷贝与深拷贝-前端爬坑-SegmentFault思否
-
学习React中ref的两个demo示例
-
7.ASP.NET MVC 5.0中的Routing【路由】
-
详解如何给React-Router添加路由页面切换时的过渡动画