1.定义:端到端的传输路径html
2.路由的结构前端
3.查询路由信息:window.locationjson
4.路由前端路由和后端路由两种后端
5.前端路由的特性api
注:这里的改变指的是js 动态改变,而不是对页面的刷新。浏览器
6.前端路由的实现方式有两种:hash,history服务器
1.apiapp
2.运做流程koa
3.特色:async
4.示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hash</title> <style> #space{height: 1500px;background-color: #f7ead9} </style> </head> <button id="btn1">assign hash</button> <button id="btn2">replace hash</button> <a href="#hash">a 标签经过href追踪id为hash的DOM 节点</a> <div id="space"></div> <h3 id="hash">hash 连接的目标</h3> <body> <script> window.addEventListener('hashchange',function(event){ console.log(event); }) document.getElementById('btn1').onclick=function(){ window.location.assign('#/hash'); } let n=0; document.getElementById('btn2').onclick=function(){ window.location.replace('#/hash'+n++); } </script> </body> </html>
1.api
2.特色
3.运做流程
4.示例
<button id="btn1">assign hash</button> <button id="btn2">replace hash</button> <body> <script> window.addEventListener('popstate',function(event){ console.log(event); }) document.getElementById('btn1').onclick=function(){ window.history.pushState(null,null,'/p1'); } let n=0; document.getElementById('btn2').onclick=function(){ window.history.replaceState(null,null,'/r'+n++); } </script> </body>
1.以koa 为例。
routeTest 文件夹是带有history 路由功能的项目文件,置入一个后端项目koa-test中,做为后端项目的一部分,此后端项目中,还会有其它的项目。
2.在app.js 轴配置后端路由
router.get('/routeTest',routeTest); router.get('/routeSecond',routeTest); async function routeTest(ctx) { await ctx.render('routeTest/index'); }
const Koa=require('Koa'); const KoaRouter=require('koa-router'); const path=require('path'); const render=require('koa-ejs'); const app=new Koa(); const router=new KoaRouter(); render(app,{ root:path.join(__dirname,'views'), layout:'layout', viewExt:'html', cache:false, debug:false }) router.get('/',index); router.get('/routeTest',routeTest); router.get('/routeSecond',routeTest); async function index(ctx) { await ctx.render('index', { msg: 'Free is my love....', }); } async function routeTest(ctx) { await ctx.render('routeTest/index'); } app.use(router.routes()).use(router.allowedMethods()); app.listen(300,()=>{ console.log('server start...'); })