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...'); })