Vue入门十、路由跳转(哈希模式)
单页一应用的路由模式有再种1、哈希模式(利用hashchange事件监听url的hash的改变)window.addEventListener("hashchange", function (e) {console.log(e)})2、history模式<!DOCTYPE html><html lang="en"><head><meta c...
·
单页一应用的路由模式有再种
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
更多推荐
已为社区贡献893条内容
所有评论(0)