vue路由原理

一、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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值