vue的两种模式:hash模式 | history模式

69 阅读1分钟
  • hash模式

    • 利用了onhashchange事件
  • history模式

    • 利用history.pushState(stateObject, title, url)来实现的

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <a href="#/home">home</a>
    <a href="#/about">about</a>
    <div id="app"></div>
    <script>
        window.onhashchange = function () {
            if (location.hash === '#/home') {
                app.innerHTML = '首页'
            } else {
                app.innerHTML = '关于'
            }
        }
    </script>
</body>

</html>