You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
exportconstROUTELIST=[{path: '/',name: 'index',component: 'This is index page'},{path: '/hash',name: 'hash',component: 'This is hash page'},{path: '/history',name: 'history',component: 'This is history page'},{path: '*',name: 'notFound',component: '404 NOT FOUND'}];
什么是路由?
前端路由的来源
前端路由的两种实现原理
1.Hash模式
2.History 模式
需要特别注意的是,调用history.pushState()或history.replaceState()不会触发popstate事件。只有在做出浏览器动作时,才会触发该事件。
两种模式优劣对比
造(cao) 一个简单的前端路由
history模式404
文件结构
1.搭建环境
废话不多说,直接上代码~
package.json
webpack.config.js
2.开撸
首先我们先初始化定义我们需要实现的功能及配置参数。
src/index.js
前面我们说了前端路由有两种实现方式。
1.定义路由列表
2.我们分别为这两种方式创建对应的类,并根据不同的mode参数进行实例化,完成webRouter类的实现。
src/routeList.js
src/hash.js
src/history.js
src/index.js
前面我们已经实现了webRouter的功能,接下来我们来实现两种方式。
因为两种模式都需要调用一个方法来实现不同路由内容的刷新,so~
index.html
js/base.js
ok,下面我们来实现两种模式。
Hash模式
src/hash.js
History模式
src/history.js
3.小功告成
源码地址
The text was updated successfully, but these errors were encountered: