23.react-router 路由
箭头函数扩展:
箭头函数:
functoin 函数名(参数){
函数体
}
箭头函数:
1、把function删掉 ,
2、参数和{}之间加上 箭头=>
简写:
1、参数的简写:只有一个参数才能简写
2、函数体的简写:只有一条语句才能简写 exp:
var f = v => alert(1);
var show = (a,b)=> {
alert(1);
return a +b
};
特殊:json
var show = ()=>{a:12,b:5};错误
var show = ()=>({a:12,b:5});——++用括号包起来++
匿名函数的定义与执行:
定义:
(function (){
alert(1);
});
执行:
1.
(function (){
alert(1);
})();
2.
(function (){
alert(11);
}());
3.
!function(){
alert(1);
}();
4.
~function(){
alert(1);
}();
5.
-function(){
alert(1);
}();
6.
+function(){
alert(1);
}();
箭头函数注意事项.特性:
1、干掉了arguments 用...args解决
exp:
<script>
//箭头函数里面没有arguments
function sum1(){
//argument
console.log(arguments);
}
console.log(sum1(1,2,3,4,5));
var sum2 = ()=>{
console.log(arguments);
}
console.log(sum2(1,2,3,4,5));
</script>
res:
解决方法:
解构赋值
<script>
//箭头函数里面没有arguments
var sum = (...args)=>{
console.log(args);
}
sum(1,2,3,4,5);
</script>
res:
2、不能用箭头函数创建类 用class解决
res:
3、this 指向父级
<script>
function Person(){
alert(this);
}
var p1 = Person();//this:Window
var p2 = new Person();//this:Object
document.onclick = Person;//this:HTMLDocument
</script>
路由:
https://reacttraining.com/react-router/
https://github.com/ReactTraining/react-router
http://react-china.org/t/react-router4/15843
Vue路由:
1、安装
cnpm i -S vue-router
2、引入
import VueRouter from "vue-router"
3、注册成为vue插件
Vue.use(VueRouter);
4、创建路由对象
let router = new VueRouter({
routes:[
{path,name,componet,componets,children}
]
});
5、注入到vue实例中
new Vue({router});
6、页面上使用路由:展现、跳转
跳转:
展现:
react路由
1、安装
npm i -S react-router-dom
2、引入
imoprt ReactRouter from "react-router-dom";
3、直接使用:包含创建路由的过程
对比:
react | vue |
---|---|
Home | Home |
{path:"/",name:"home",componet:Home} | |
exact 控制匹配到/路径时不会再继续向下匹配, | |
path 标识路由的路径, | |
component 表示路径对应显示的组件。 | |
HashRouter和BrowserRouter 是标签(使用方式:包的内容只能有一个子节点) | mode:"hash/history" |
虚拟路径:basename="/basename" | 虚拟路径:base:"/base" |
Switch常常会用来包裹Route/Redirect,它里面不能放其他元素,用来只显示一个路由
react vue
<Link to="/">Home</Link> <router-link to="/">Home</router-link>
<Route exact path="/" component={Home} /> {path:"/",name:"home",componet:Home}
exact 控制匹配到/路径时不会再继续向下匹配,
path 标识路由的路径,
component 表示路径对应显示的组件。
HashRouter和BrowserRouter 是标签 mode:"hash/history"
****使用方式:包的内容只能有一个子节点
虚拟路径:
basename="/basename" base:"/base"
Switch常常会用来包裹Route/Redirect,它里面不能放其他元素,用来只显示一个路由。
exp1:
Link,Route
import React, { Component } from 'react';
import {Route,Link,Switch,Redirect} from "react-router-dom";
//import {HashRouter as Router,Route,Link,Switch,Redirect} from "react-router-dom";
const Home = ()=> <div>首页</div>
const News = ()=> <div>新闻</div>
const Users = ()=> <div>用户</div>
class App extends Component {
render() {
return (
<div className="App">
<a href="/home">首页</a>
<a href="/news">新闻</a>
<a href="/users">用户</a>
<hr />
<Link to="/home">首页</Link>
<Link to="/news">新闻</Link>
<Link to="/users">用户</Link>
<hr />
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/home" component={Home}/>
<Route path="/news" component={News}/>
<Route path="/users" component={Users}/>
{/* <Route component={Home} />
*/}
{/*<Route path="/*" component={News}/> */}
<Redirect to="/" />
</Switch>
</div>
);
}
}
export default App;
exp2:
props.location:
{
hash:"",
key:"579ijl",
pathname:"/news",
search:"?id=111",
state:undefined
}
mport React, { Component } from 'react';
import {BrowserRouter as Router,Route,Link,Switch,Redirect} from "react-router-dom";
const Home = (props)=> {
console.log("Home:",props);
return <div>首页</div>;
};
const News = (props)=> {
console.log("news:",props);
return <div>新闻</div>;
};
const Users = (props)=> {
console.log("Users:",props);
return <div>用户</div>;
};
const refCallback = node => {
// `node` refers to the mounted DOM element or null when unmounted
console.log("node",node);
return <div>用户函数</div>;
}
class App extends Component {
render() {
return (<Router>
<div className="App">
<Link to="/home">首页</Link>
<Link to="/news?id=111">新闻</Link>
<Link to={{
hash:"",
pathname:"/users",
search:"?username=aaa&password=123",
state:{ fromDashboard: true },
}}>用户</Link>
<Link to="/refCallback" innerRef={refCallback} >用户函数</Link>
<hr />
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/home" component={Home}/>
<Route path="/news" component={News}/>
<Route path="/users" component={Users}/>
<Route path="/refCallback" component={refCallback}/>
<Redirect to="/"/>
</Switch>
</div>
</Router>
);
}
}
export default App;
exp3:
const Users = ({match})=>{
console.log("Users:",match);
return <div>用户-----{match.params.id}</div>
}
class App extends Component{
render(){
return (
<div className = "App">
<Link to="/users/111">用户111</Link>
<Link to="/users/222">用户222</Link>
<Link to="/users/333">用户333</Link>
<hr/>
<Switch>
<Route path="/users/:id" component={Users}/>
</Switch>
</div>
)
}
}
res:
exp4:
import React, { Component } from 'react';
import {Route,Link,NavLink,Switch,Redirect} from "react-router-dom";
const Home = ()=> <div>首页</div>
const News = ()=> <div>新闻</div>
const Users = ()=> <div>用户</div>
class App extends Component {
render() {
return (
<div className="App">
<Link to="/home">首页</Link>
<Link to="/news">新闻</Link>
<Link to="/users">用户</Link>
<hr />
<NavLink activeClassName="selected" to="/home">首页</NavLink> {/*点击添加class和属性*/}
<NavLink activeClassName="selected" to="/news">新闻</NavLink>
<NavLink to="/users" activeStyle={{
fontWeight: 'bold',
color: 'red'
}}>用户</NavLink>
<hr />
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/home" component={Home}/>
<Route path="/news" component={News}/>
<Route path="/users" component={Users}/>
<Redirect to="/"/>
</Switch>
</div>
);
}
}
export default App;
res:
路由传参:
1、?
2、params
props.match.params.xxx
props.match.path -->"/users/:id" ----> route
props.match.url -->"/users/111" ----> link
exp5:
import React, { Component } from 'react';
import { Route,NavLink,Switch,Redirect} from "react-router-dom";
import "./index.css";
const Home = () =><div>首页</div>
const News = () =><div>新闻</div>
const Users = ({match}) =>{
console.log(111,match)
return (<div>
<NavLink to={`${match.url}/111`} >用户111</NavLink>
<NavLink to={`${match.url}/222`} >用户222</NavLink>
<NavLink to={`${match.url}/333`} >用户333</NavLink>
<Route path={`${match.url}/:id`} component={USerDetail}/>
</div>)
}
const USerDetail = ({match}) =>{
return (
<div>
<NavLink to={`${match.url}/info`}>用户信息</NavLink>
<NavLink to={`${match.url}/pass`}>用户密码</NavLink>
<Route path={`${match.path}/info`} component={UserInfo} />
<Route path={`${match.path}/pass`} component={UserPass} />
</div>
)
}
const UserInfo = ({match}) => {
return (
<div>
UserInfo --- {match.url}
</div>
);
}
const UserPass = ({match}) => {
return (
<div>
UserPass --- {match.url}
</div>
);
}
class App extends Component{
render(){
return (
<div className = "App">
<NavLink to="/home">首页</NavLink>
<NavLink to="/news">新闻</NavLink>
<NavLink to="/users">用户</NavLink>
<hr/>
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/home" component={Home}/>
<Route path="/news" component={News}/>
<Route path="/users" component={Users}/>
<Redirect to = "/" />
</Switch>
</div>
)
}
}
export default App;
res:
获取数据: 都是从组件的属性上去获取 props
props:组成{history,location,match}
history: {go,goBack,goForward} 做跳转用
location:{hash,pathname,search,stat}
match:{params,path,url}
exp6:
import React, { Component } from 'react';
import { BrowserRouter as Router,Route,Link,Switch,withRouter} from "react-router-dom";
function Home(){
return <div>首页</div>
}
function News(){
return <div>新闻</div>
}
function Users(){
return <div>用户</div>
}
class App extends Component {
back(){
//window.history.back();
//window.history.go(-1);
console.log(this.props);
this.props.history.go(-1);
}
forward(){
//window.history.forward();
//window.history.go(1);
this.props.history.go(1);
}
push(){
// window.location = "/"
this.props.history.push("/");
}
render() {
return ( <Router>
<div className="App">
<input onClick={()=> this.back()} type="button" value="back" />
<input onClick={()=> this.forward()} type="button" value="forward" />
<input onClick={()=> this.push()} type="button" value="回到首页" />
<hr />
<Link to="/home">首页</Link>
<Link to="/news">新闻</Link>
<Link to="/users">用户</Link>
<hr />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/home" component={Home} />
<Route path="/news" component={News} />
<Route path="/users" component={Users} />
<Route path="/*" component={News} />
</Switch>
</div>
</Router>
);
}
}
export default withRouter(App);
res:

withRouter: 功能 为组件添加路由信息{history,location,match}---> this.props
运行条件: 必须在 Router下面 不能在Router外面!
** exact Switch
https://reacttraining.com/react-router/core/api/Router
1.
什么叫 hash 地址,hash 地址就是指 # 号后面的 url。
假如有一个 Link 标签,点击后跳转到 /abc/def。
BrowserRouter: http://localhost:8080/abc/def
HashRouter: http://localhost:8080/#/abc/def
如果有服务器端的动态支持,建议使用 BrowserRouter,否则建议使用 HashRouter。
原因在于,如果是单纯的静态文件,假如路径从 / 切换到 /a 后,此时刷新页面,页面将无法正常访问。
2.
将“URL”的历史记录保存在内存中(不读取或写入地址栏)的<路由器>。在测试和非浏览器环境(如React Native)中很有用
3.
一个从不改变位置的。
当用户实际上没有点击时,这在服务器端渲染场景中很有用,因此该位置从未实际改变。因此,名称:static。当您只需插入一个位置并在渲染输出上作出断言时,它也可用于简单的测试。
以下是一个示例节点服务器,为发送302状态代码,并为其他请求发送常规HTML
4.
一个<Router和Android应用程序的> 。
import { NativeRouter } from 'react-router-native'
<NativeRouter>
<App/>
</NativeRouter>
23.react-router 路由的更多相关文章
- < react router>: (路由)
< react router> (路由): 思维导图: Atrial 文件夹下的index.js 文件内容: import React, { Component } from 'rea ...
- React初识整理(四)--React Router(路由)
官网:https://reacttraining.com/react-router 后端路由:主要做路径和方法的匹配,从而从后台获取相应的数据 前端路由:用于路径和组件的匹配,从而实现组件的切换. 如 ...
- react router路由传参
今天,我们要讨论的是react router中Link传值的三种表现形式.分别为通过通配符传参.query传参和state传参. ps:进入正题前,先说明一下,以下的所有内容都是在react-rout ...
- 【react router路由】<Router> <Siwtch> <Route>标签
博客 https://www.jianshu.com/p/ed5e56994f13?from=timeline 文档 http://react-guide.github.io/react-router ...
- react ---- Router路由的使用和页面跳转
React-Router的中文文档可以参照如下链接: http://react-guide.github.io/react-router-cn/docs/Introduction.html 首先,我们 ...
- React Router路由传参方式总结
首先我们要知道一个前提,路由传递的参数我们可以通过props里面的属性来获取.只要组件是被<Router>组件的<component>定义和指派的,这个组件自然就有了props ...
- react router 4.0以上的路由应用
thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr& ...
- react router @4 和 vue路由 详解(七)react路由守卫
完整版:https://www.cnblogs.com/yangyangxxb/p/10066650.html 12.react路由守卫? a.在之前的版本中,React Router 也提供了类似的 ...
- react router @4 和 vue路由 详解(全)
react router @4 和 vue路由 本文大纲: 1.vue路由基础和使用 2.react-router @4用法 3.什么是包容性路由?什么是排他性路由? 4.react路由有两个重要的属 ...
- React Router 4.0 实现路由守卫
在使用 Vue 或者 Angular 的时候,框架提供了路由守卫功能,用来在进入某个路有前进行一些校验工作,如果校验失败,就跳转到 404 或者登陆页面,比如 Vue 中的 beforeEnter 函 ...
随机推荐
- Class:DbConnectionManipulator.cs
ylbtech-Class:DbConnectionManipulator.cs 1.返回顶部 1.DbConnectionManipulator.cs using System; using Sys ...
- MultipartFile文件编码判断
MultipartFile文件编码判断 搜索:Java 判断文件的字符集编码 https://blog.csdn.net/top_code/article/details/8891796 但是在Mul ...
- 【ASP.NET Core】浅说目录浏览
何谓“浅说”?就是一句话说不完,顶多两句话就介绍完毕,然后直接给上实例的解说方式.化繁为简,从七千年前到现在,从老祖宗到咱们,一直都在追求的理想目标,尽可能把复杂的东西变成简单的. 老周告诉你一个可以 ...
- Python 爬虫实例(9)—— 搜索 爬取 淘宝
# coding:utf- import json import redis import time import requests session = requests.session() impo ...
- ANTLR v4 专业术语集
记录<The Definitive ANTLR 4 Reference>中出现的专业术语: grammar 文法,一种形式化(formal)的语言描述. syntax 语法 phrase ...
- Docker的学习
学习地址:http://blog.51cto.com/lizhenliang 和 他的视频 一 Docker 的介绍和安装 二 镜像管理 三 容器管理 四 管理应用程序数据 五 使用Docker知 ...
- TF常用知识
命名空间及变量共享 # coding=utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt; ...
- 【Unity】项目工程源码
Unity开发者俱乐部 http://blog.csdn.net/dingxiaowei2013/article/details/50605208 游戏蛮牛 9秒社团 6m5m raywenderli ...
- hdoj:2083
简易版之最短距离 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- Oracle中exists替代in语句
大家都知道exists的速度要比in的速度快,也知道exists函数返回一个布尔值,也就是说exists函数里最后要是 a.id =b.id类似这种方式结束. 例如: SELECT * FROM TB ...