单页一应用的路由模式有再种
1、哈希模式(利用hashchange事件监听url的hash的改变)
window.addEventListener("hashchange", function (e) {console.log(e)})
2、history模式

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script type="text/javascript" src="vue.js"></script>
    <a href="#/login">登录</a>
    <a href="#/register">注册</a>
    <div id="app"></div>
    <script type="text/javascript">
        var appdiv = document.getElementById("app")
        window.addEventListener("hashchange", function (e) {
            console.log(e.newURL)
                        // #/login
            console.log(location.hash)
            switch (location.hash) {
                case "#/login":appdiv.innerHTML="我是登录页面";
                    break;
                case "#/register":appdiv.innerHTML="我是注册页面";
                break;
            }
        })
    </script>

</body>
</html>

转载于:https://blog.51cto.com/12012821/2401882

Logo

前往低代码交流专区

更多推荐