×î½ü¿´µ½vue-routerµÄHTML5 History ģʽ·ÓɵÄʵÏÖ£¬È»ºó˳±ãÓÖÈ¥Ñо¿ÁËÒ»ÏÂHTML5 µÄ History£¬ÒÔÏÂÊÇ×Ô¼ºµÄһЩÀí½â£¬Ë³±ãÓÃjqueryд Ò»¸öʵÏÖÀàËÆvue-routerÀïÃæHTML5 History ģʽ·ÓÉÆ÷,ÒÔ´ïµ½Á·Á·ÊÖ£¬ÊìϤÊìϤµÄÄ¿µÄ¡£
Ò»¡¢history.pushState
history.pushState(state, title, url);
ÉÏÃæµÚÒ»ºÍµÚ¶þ¸ö²ÎÊý¿ÉÒÔΪ¿Õ£¬Ö÷Òª¾ÍÊǵÚÈý¸ö²ÎÊý£¬±íʾÐÂÀúÊ·¼Í¼µÄµØÖ·£¬ä¯ÀÀÆ÷ÔÚµ÷ÓÃpushState()·½·¨ºó²»»áÈ¥¼ÓÔØÕâ¸öURL£¬ÐµÄURL²»Ò»¶¨ÒªÊǾø¶ÔµØÖ·£¬Èç¹ûËüÊÇÏà¶ÔµÄ£¬ËüÒ»¶¨ÊÇÏà¶ÔÓÚµ±Ç°µÄURL
¶þ¡¢history.replaceState
history.replaceState(state, title, url);
window.history.replaceState ºÍ window.history.pushState ÀàËÆ£¬²»Í¬Ö®´¦ÔÚÓÚ replaceState ²»»áÔÚ window.history ÀïÐÂÔöÀúÊ·¼Ç¼µã£¬ÆäЧ¹ûÀàËÆÓÚ window.location.replace(url) £¬¶¼ÊDz»»áÔÚÀúÊ·¼Ç¼µãÀïÐÂÔöÒ»¸ö¼Ç¼µãµÄ¡£
Èý¡¢window.onpopstate
À´¼àÌýurlµÄ±ä»¯
window.addEventListener("popstate", function() {
var currentState = history.state;
/*
* ´¥·¢Ê¼þºóÒªÖ´ÐеijÌÐò
*/
});
//»òÕß
window.onpopstate = function(){}
javascript½Å±¾Ö´ÐÐ window.history.pushState ºÍ window.history.replaceState ²»»á´¥·¢ onpopstate ʼþ£¬ÔÚä¯ÀÀÆ÷µã»÷Ç°½ø»òÕߺóÍ˻ᴥ·¢
¹È¸èä¯ÀÀÆ÷ºÍ»ðºüä¯ÀÀÆ÷ÔÚÒ³ÃæµÚÒ»´Î´ò¿ªµÄ·´Ó¦ÊDz»Í¬µÄ£¬¹È¸èä¯ÀÀÆ÷Ææ¹ÖµÄÊǻش¥·¢ onpopstate ʼþ£¬¶ø»ðºüä¯ÀÀÆ÷Ôò²»»á
ËÄ¡¢ÏÂÃæÌùÒ»¸öÀàËÆvue-routerµÄHTML5ģʽµÄÀý×Ó£¬´¿Êô¼ÓÉîÀí½â£¬Ð´µÄºÜ´Ö²Ú¡£
HTML5 History ģʽ(µÚ¶þ°æ) .container-bg .pagination .pagination li123 history.replaceState(´Ë¶ÔÏóÓÃÀ´±£´æÏÂÃæpushStateµÄURL×÷ΪkeyÖµ£¬ajaxÒª²éѯµÄIDΪval *ÀýÈ磺searchObject = {"http://127.0.0.1:3000/lmw/0":0}(){ :query); (data){ JSON.parse(data); ){ ele } $().html(ele) }) }; (){ ).index(); $.get((data){ JSON.parse(data); ){ ele } $().html(ele) history.pushState({pageIndex : query); query }) }) (history.pushState) { window.addEventListener(() { factory() }); factory() };
˳±ãÌùÒ»¸önode.jsÖеÄserver´úÂ룬ΪÁ˲âÊÔ£¬ºÜËæÒâ¼òµ¥µÄдÁËÒ»¸ö
var fs = require('fs') var path = require('path') var express = require('express') var app = express(); app.use(express.static('./public')); var router = express.Router(); router.get('/page',function(req,res){ var page = req.query.page try{ var text = fs.readFileSync('./data'+page+'.json'); res.json(text.toString()) }catch(err){ res.send('¹þ¹þ£¡Éµ±Æ£¬Ã»ÓÐÀ£¡') } }) app.use(router) app.listen(3000)
×¢Ò⣺history.pushState({pageIndex : 1}, "", "http://127.0.0.1:3000/lmw/"+query)ÕâÀïµÚÈý¸ö²ÎÊýдÁËÍêÕûµÄ¾ø¶Ô·¾¶£¬Èç¹ûд³É"/lmw/"+queryÕâÑùµÄÏà¶Ô·¾¶£¬»áËæ×ÅqueryÖµµÃÔö¼ÓÎÞÏÞÔÚurlºóÃæ×·¼Ó£¬ÒòΪÏà¶Ô·¾¶ËüÒ»¶¨ÊÇÏà¶ÔÓÚµ±Ç°µÄURL·þÎñ¶Ë·ÅÁËdata0.json,data1.json,data2.jsonÀ´Ä£ÄâÒ»ÏÂÊý¾Ý¿âÈ¡Êý¾Ý£¬·þÎñÆ÷¸ü¾ßÇ°¶Ë´«À´µÄindexÖµ£¨0/1/2£©£¬À´Æ¥Åä¶ÁÈ¡data*.jsonÎļþ£¬ÔÙ·¢¸øÇ°¶Ë
¡¡