博客列表 >路由原理与实现

路由原理与实现

王佳祥
王佳祥原创
2020年09月12日 07:45:59787浏览

路由原理与实现

一、原生路由原理及实现

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>路由的原理</title>
  7. <script src="lib/vue.js"></script>
  8. </head>
  9. <body>
  10. <a href="#1">新闻1</a>
  11. <a href="#2">新闻2</a>
  12. <a href="#3">新闻3</a>
  13. <!-- 根据路由/锚点不同 -->
  14. <!-- <h3 id="story1" style="margin-top: 1200px">故事的内容是这样的......</h3>
  15. <h3 id="story2" style="margin-top: 1900px">故事的内容是这样的......</h3> -->
  16. <div id="app"></div>
  17. <!-- 404 -->
  18. <div id="page404" style="display: none">这个故事说完了!</div>
  19. </body>
  20. <script>
  21. //原生的SPA(单页面应用)的路由实现方式
  22. //基于锚点实现
  23. //(一)路由的原理
  24. //实现不刷新页面,就可以动态实现数据的加载,达到SPA的效果
  25. /* console.log(window.location.hash);获取url中的锚点
  26. console.log(document.querySelector("a:first-of-type").href.substr(-2));
  27. console.log(document.querySelector("a:nth-of-type(2)").href.substr(-2));
  28. console.log(document.querySelector("a:last-of-type").href.substr(-2)); */
  29. //window.addEventListener("hashchange", () => console.log("hash变化了"));
  30. //(二)路由的实现
  31. //1.创建dom节点,并注册到路由表中
  32. const app = document.querySelector("#app");
  33. const div1 = document.createElement("div");
  34. div1.innerHTML = "1.习近平的2020·八月:谋划“新发展阶段”";
  35. const div2 = document.createElement("div");
  36. div2.innerHTML = "2.中国关于联合国成立75周年立场文件";
  37. const div3 = document.createElement("div");
  38. div3.innerHTML = "3.习近平总书记关切事丨敬礼!最温暖的陪伴";
  39. //2.注册到路由表中
  40. //路由表
  41. const routeTable = {
  42. 1: div1,
  43. 2: div2,
  44. 3: div3,
  45. };
  46. //生成路由
  47. function route(container) {
  48. //1.:获取到路由
  49. let num = window.location.hash.substr(1);
  50. console.log(num);
  51. //2.默认路由
  52. num = num || 1;
  53. console.log(num);
  54. //3.根据路由表获取对应的内容
  55. let content = routeTable[num];
  56. //4.如果路由表不存在指定的内容,获取到404页面
  57. if (!content) content = document.querySelector("#page404");
  58. content.style.display = "block";
  59. //5.先将容器清空
  60. container.innerHTML = null;
  61. //6.将路由对应的页面渲染到指定的容器中
  62. container.appendChild(content);
  63. }
  64. //路由的初始化/显示默认页面
  65. route(app);
  66. //监听路由的变化/监听的锚点的变化
  67. window.addEventListener("hashchange", () => route(app));
  68. </script>
  69. </html>


二、Vue路由的原理

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Vue路由的原理</title>
  7. <script src="lib/vue.js"></script>
  8. <!-- 路由包 -->
  9. <script src="lib/vue-router-dev/dist/vue-router.js"></script>
  10. </head>
  11. <body>
  12. <div id="app">
  13. <!-- 1.使用router-link组件来生成导航 -->
  14. <router-link to="/news1">新闻1</router-link>
  15. <router-link to="/news2">新闻2</router-link>
  16. <router-link to="/news3">新闻3</router-link>
  17. <!-- 2.路由内容的渲染 -->
  18. <router-view></router-view>
  19. </div>
  20. </body>
  21. <script>
  22. const news1 = {
  23. template: "<p>1.从前有座山.....</p>",
  24. };
  25. const news2 = {
  26. template: "<p>2.龟兔赛跑.....</p>",
  27. };
  28. const news3 = {
  29. template: "<p>3.和氏璧.....</p>",
  30. };
  31. //注册路由
  32. const router = new VueRouter({
  33. routes: [
  34. //是一个对象数组,每个对象对应一个路由
  35. { path: "/news1", component: news1 },
  36. { path: "/news2", component: news2 },
  37. { path: "/news3", component: news3 },
  38. ],
  39. });
  40. //vue实例
  41. const vm = new Vue({
  42. //将路由注册到挂载点中
  43. //当属性名与变量同名,只需要写一个
  44. router: router,
  45. //router,
  46. }).$mount("#app");
  47. </script>
  48. </html>


学习总结

1.原生路由

  • window.location.hash获取url中的锚点

  • hashchange参数来监听锚点的变化

  • 实现路由的步骤:

    1.创建dom节点

    2.注册到路由表中

    3.生成路由(获取到路由、设置默认路由、根据路由表获取对应的内容、清空容器、将路由对应的页面渲染到指定的容器中)

    4.路由初始化/显示默认页面

    5.监听路由的变化/监听的锚点的变化

2.vue路由

  1. const router = new VueRouter({
  2. routes: [//是一个对象数组,每个对象对应一个路由],
  3. });
  • 最后将路由注册到挂载点中

  • 当属性名与变量同名,只需要写一个

  • $mount():手动挂载

3.git基础

  • git:免费,开源的分布式版本控制系统

  • git 中的一些术语:

    • 版本库

      • 本地版本库
      • 远程版本库
    • 工作区

    • 暂存区

  • git 流程

    • 在工作中编程
    • 将代码先放到暂存区
    • 将暂存区内容一次性的提交到本地版本库
    • 将本地的版本库内容提交到远程的版本库


  • cd 目录 切换到需要使用git进行版本控制的项目目录中

  • 配置用户名和邮箱

  1. git config --global user.name '用户名'
  2. git config --global user.email '邮箱'
  • git 版本库的初始化:创建版本库
  1. git init
  • 将工作区的已经修改过的文件,提交到本地版本库中的暂存区
  1. git add . # 一次性全部提交完毕 add命令可将该文件添加到暂存区
  2. git status # 查看状态
  • 在将所有的内容从本地暂存区一次性的提交到本地的版本库
  1. git commit -m '2020-09-10 22:02' //'注释'
  • 添加远程版本库
  1. git remote add origin https://gitee.com/bnkt/git-edu.git
  • 查看远程仓库
  1. git remote -v
  • 提交到远程仓库
  1. git push origin master //指定远程仓库名和分支名
  2. git push -u origin master //将本地的master分支推送到origin主机,同时指定origin为默认主机
  3. git push -f origin master //本地强制上传到远程,把远程的覆盖
  • 从远程仓库拉取内容
  1. git pull https://gitee.com/jia-xiang/git-edu.git
  2. git pull origin master






  • 从远程仓库拉取内容




声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议