• hash: 使用 URL hash 值来作路由。支持所有浏览器,包括不支持 HTML5 History Api 的浏览器。
    1. document.addEventListener('DOMContentLoaded', function () {
    2. console.log(location.hash);
    3. })
    4. window.addEventListener('hashchange', function (event) {
    5. const { newURL, oldURL } = event
    6. console.log('listen', newURL, oldURL, event)
    7. const newHash = newURL.split('#').pop()
    8. document.getElementById('app').innerHTML = newHash
    9. })
    10. function routeTo (targetName) {
    11. console.log('click', targetName)
    12. location.hash = targetName
    13. }

    image.png

    1. document.addEventListener('DOMContentLoaded', function () {
    2. console.log(location.pathname);
    3. })
    4. window.addEventListener('popstate', function (event) {
    5. var { state } = event
    6. console.log('listen', state, event)
    7. changeView(JSON.stringify(state))
    8. })
    9. function routeTo (targetName) {
    10. console.log('click', targetName)
    11. push(targetName)
    12. }
    13. function push (targetName) {
    14. var state = { page_name: targetName };
    15. var title = '';
    16. var url=`${targetName}.html`;
    17. history.pushState(state, title, url)
    18. changeView(targetName)
    19. }
    20. function replace(targetName) {
    21. var state = { page_name: targetName };
    22. var title = '';
    23. var url=`${targetName}.html`;
    24. history.replaceState(state, title, url)
    25. changeView(targetName)
    26. }
    27. function changeView(val) {
    28. document.getElementById('app').innerHTML = val
    29. }
    • abstract: 支持所有 JavaScript 运行环境,如 Node.js 服务器端。如果发现没有浏览器的 API,路由会自动强制进入这个模式。

    代码:https://gitee.com/daaasheng/whiteboard/tree/master/vue-router/mode

    参考:https://router.vuejs.org/zh/api/#mode