一、hash模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script>
//自定义三个UI组件
var UI = `
<div>
<h1>我是根页面,00000</h1>
</div>
`;
var UI1 = `
<div>
<h1>我是一页面,111111</h1>
</div>
`;
var UI2 = `
<div>
<h1>我是二页面,2222222</h1>
</div>
`;
//自定义路由规则
var router = [
{ path: "/", component: UI },
{ path: "/u1", component: UI1 },
{ path: "/u2", component: UI2 },
]
//通过路由规则来显示UI组件
function render() {
//获取hash值的路径
var hashv = location.hash;
if (hashv == "") location.hash = "/"; //页面刚刚加载时,设置为/路径
var path = location.hash.slice(1); //获取去掉了#号后的实际路径
//根据hash值路径渲染页面
var content = document.getElementById("app");
router.forEach(function (ele) {
if (ele.path == path) {
content.innerHTML = ele.component;
}
})
}
render(); //默认执行渲染函数 render
//监听hash值变化,同步渲染页面组件
window.addEventListener("hashchange", function () {
render();
})
</script>
</body>
</html>
二、点击路由
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 通过类似于router-link来实现hash模式的路由触发 -->
<button to="/">UI</button>
<button to="/u1">UI1</button>
<button to="/u2">UI2</button>
<content id="content"> </content>
</div>
<script>
//自定义三个UI组件
var UI = `
<div>
<h1>我是根页面,00000</h1>
</div>
`;
var UI1 = `
<div>
<h1>我是一页面,111111</h1>
</div>
`;
var UI2 = `
<div>
<h1>我是二页面,2222222</h1>
</div>
`;
//自定义路由规则
var router = [
{ path: "/", component: UI },
{ path: "/u1", component: UI1 },
{ path: "/u2", component: UI2 },
]
//通过路由规则来显示UI组件
function render() {
//获取hash值的路径
var hashv = location.hash;
if (hashv == "") location.hash = "/"; //页面刚刚加载时,设置为/路径
var path = location.hash.slice(1); //获取去掉了#号后的实际路径
//根据hash值路径渲染页面
var content = document.getElementById("content");
router.forEach(function (ele) {
if (ele.path == path) {
content.innerHTML = ele.component;
}
})
}
render(); //默认执行渲染函数 render
//监听hash值变化,同步渲染页面组件
window.addEventListener("hashchange", function () {
render();
})
//监听含有to属性的按钮被点击
document.getElementById("app").onclick = function () {
var path = event.target.getAttribute("to"); //获取点击按钮的路径
if (path) {
location.hash = path;
}
}
</script>
</body>
</html>
三、history模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<!-- 通过类似于router-link来实现hash模式的路由触发 -->
<button to="/">UI</button>
<button to="/u1">UI1</button>
<button to="/u2">UI2</button>
<content id="content"> </content>
</div>
<script>
//自定义三个UI组件
var UI = `
<div>
<h1>我是根页面,00000</h1>
</div>
`;
var UI1 = `
<div>
<h1>我是一页面,111111</h1>
</div>
`;
var UI2 = `
<div>
<h1>我是二页面,2222222</h1>
</div>
`;
//自定义路由规则
var router = [
{ path: "/", component: UI },
{ path: "/u1", component: UI1 },
{ path: "/u2", component: UI2 },
]
//通过路由规则来显示UI组件
function render() {
//页面刚刚加载时,设置为/路径
if (location.pathname.match(/\.html/) != null) {
history.pushState(null, null, "/");
}
//获取路径名
var pathname = location.pathname;
//根据pathname渲染页面
var content = document.getElementById("content");
router.forEach(function (ele) {
if (ele.path == pathname) {
content.innerHTML = ele.component;
}
})
}
render(); //默认执行渲染函数 render
//监听含有to属性的按钮被点击
document.getElementById("app").onclick = function () {
var path = event.target.getAttribute("to"); //获取点击按钮的路径
if (path) {
history.pushState(null, null, path);
render();
}
}
</script>
</body>
</html>