Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vue路由原理 #22

Open
Wscats opened this issue Dec 3, 2018 · 0 comments
Open

vue路由原理 #22

Wscats opened this issue Dec 3, 2018 · 0 comments

Comments

@Wscats
Copy link
Owner

Wscats commented Dec 3, 2018

hashchange

现在大部分单页面应用程序的路由切换都是以下这种方式

http://www.xxx.com/#/index

这种 #。后面 hash 值的变化,并不会导致浏览器向服务器发出请求,浏览器不发出请求,也就不会刷新页面。另外每次 hash 值的变化,还会触发 hashchange 这个事件,通过这个事件我们就可以知道 hash 值发生了哪些变化。然后我们便可以监听 hashchange 来实现更新页面部分内容的操作

<body>
    <a href="#/index">index</a>
    <a href="#/home">home</a>
    <script>
        function matchAndUpdate() {
            // 匹配 hash 做 dom 更新操作
            console.log("路由切换了");
        }
        window.addEventListener('hashchange', matchAndUpdate)
    </script>
</body>

示例

哈希路由的简单实现

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant