[摘要]/; typeof this.routes[this.curHash] === function && this.routes[this.curHash]();};index....
'/'; typeof this.routes[this.curHash] === 'function' && this.routes[this.curHash]();
};
index.html
<ul> <li><a href='#blue'></a></li> <li><a href='#yellow'></a></li> <li><a href='#red'></a></li></ul>
index.js
var router = new FrontRouter();
router.route('blue', function() {
document.body.style.backgroundColor = 'blue';
});
router.route('yellow', function() {
document.body.style.backgroundColor = 'yellow';
});
router.route('red', function() {
document.body.style.backgroundColor = 'red';
});
一点总结
应用场景
前端路由大部分的应用场景,就是我们现在熟知的单页应用SPA。
不存在纯前端路由
我们此前所描述的前端路由,建立在已经打开了一个初始页面基础之上,然后在这个页面之内进行页面替换。然而,我们如何进入这个初始页面?仅靠前端路由肯定是力所不及。我们至少要向后端发送一次http请求,接收所需要加载的页面不是吗?
所以,我们并不能抛弃后端路由部分。这也意味着,我们需要和后端确认各自的分工,哪些url归前端解析,哪些归后台解析。
网站建设是一个广义的术语,涵盖了许多不同的技能和学科中所使用的生产和维护的网站。
关键词:啥是前端路由及解释