亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 55
粉絲 3
評(píng)論 0
訪問量 69568
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
路由原理與實(shí)現(xiàn)
王佳祥
原創(chuàng)
993人瀏覽過

路由原理與實(shí)現(xiàn)

一、原生路由原理及實(shí)現(xiàn)

  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. <!-- 根據(jù)路由/錨點(diǎn)不同 -->
  14. <!-- <h3 id="story1" style="margin-top: 1200px">故事的內(nèi)容是這樣的......</h3>
  15. <h3 id="story2" style="margin-top: 1900px">故事的內(nèi)容是這樣的......</h3> -->
  16. <div id="app"></div>
  17. <!-- 404 -->
  18. <div id="page404" style="display: none">這個(gè)故事說完了!</div>
  19. </body>
  20. <script>
  21. //原生的SPA(單頁面應(yīng)用)的路由實(shí)現(xiàn)方式
  22. //基于錨點(diǎn)實(shí)現(xiàn)
  23. //(一)路由的原理
  24. //實(shí)現(xiàn)不刷新頁面,就可以動(dòng)態(tài)實(shí)現(xiàn)數(shù)據(jù)的加載,達(dá)到SPA的效果
  25. /* console.log(window.location.hash);獲取url中的錨點(diǎn)
  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. //(二)路由的實(shí)現(xiàn)
  31. //1.創(chuàng)建dom節(jié)點(diǎn),并注冊(cè)到路由表中
  32. const app = document.querySelector("#app");
  33. const div1 = document.createElement("div");
  34. div1.innerHTML = "1.習(xí)近平的2020·八月:謀劃“新發(fā)展階段”";
  35. const div2 = document.createElement("div");
  36. div2.innerHTML = "2.中國關(guān)于聯(lián)合國成立75周年立場(chǎng)文件";
  37. const div3 = document.createElement("div");
  38. div3.innerHTML = "3.習(xí)近平總書記關(guān)切事丨敬禮!最溫暖的陪伴";
  39. //2.注冊(cè)到路由表中
  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.默認(rèn)路由
  52. num = num || 1;
  53. console.log(num);
  54. //3.根據(jù)路由表獲取對(duì)應(yīng)的內(nèi)容
  55. let content = routeTable[num];
  56. //4.如果路由表不存在指定的內(nèi)容,獲取到404頁面
  57. if (!content) content = document.querySelector("#page404");
  58. content.style.display = "block";
  59. //5.先將容器清空
  60. container.innerHTML = null;
  61. //6.將路由對(duì)應(yīng)的頁面渲染到指定的容器中
  62. container.appendChild(content);
  63. }
  64. //路由的初始化/顯示默認(rèn)頁面
  65. route(app);
  66. //監(jiān)聽路由的變化/監(jiān)聽的錨點(diǎn)的變化
  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組件來生成導(dǎo)航 -->
  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.路由內(nèi)容的渲染 -->
  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. //注冊(cè)路由
  32. const router = new VueRouter({
  33. routes: [
  34. //是一個(gè)對(duì)象數(shù)組,每個(gè)對(duì)象對(duì)應(yīng)一個(gè)路由
  35. { path: "/news1", component: news1 },
  36. { path: "/news2", component: news2 },
  37. { path: "/news3", component: news3 },
  38. ],
  39. });
  40. //vue實(shí)例
  41. const vm = new Vue({
  42. //將路由注冊(cè)到掛載點(diǎn)中
  43. //當(dāng)屬性名與變量同名,只需要寫一個(gè)
  44. router: router,
  45. //router,
  46. }).$mount("#app");
  47. </script>
  48. </html>


學(xué)習(xí)總結(jié)

1.原生路由

  • window.location.hash獲取url中的錨點(diǎn)

  • hashchange參數(shù)來監(jiān)聽錨點(diǎn)的變化

  • 實(shí)現(xiàn)路由的步驟:

    1.創(chuàng)建dom節(jié)點(diǎn)

    2.注冊(cè)到路由表中

    3.生成路由(獲取到路由、設(shè)置默認(rèn)路由、根據(jù)路由表獲取對(duì)應(yīng)的內(nèi)容、清空容器、將路由對(duì)應(yīng)的頁面渲染到指定的容器中)

    4.路由初始化/顯示默認(rèn)頁面

    5.監(jiān)聽路由的變化/監(jiān)聽的錨點(diǎn)的變化

2.vue路由

  1. const router = new VueRouter({
  2. routes: [//是一個(gè)對(duì)象數(shù)組,每個(gè)對(duì)象對(duì)應(yīng)一個(gè)路由],
  3. });
  • 最后將路由注冊(cè)到掛載點(diǎn)中

  • 當(dāng)屬性名與變量同名,只需要寫一個(gè)

  • $mount():手動(dòng)掛載

3.git基礎(chǔ)

  • git:免費(fèi),開源的分布式版本控制系統(tǒng)

  • git 中的一些術(shù)語:

    • 版本庫

      • 本地版本庫
      • 遠(yuǎn)程版本庫
    • 工作區(qū)

    • 暫存區(qū)

  • git 流程

    • 在工作中編程
    • 將代碼先放到暫存區(qū)
    • 將暫存區(qū)內(nèi)容一次性的提交到本地版本庫
    • 將本地的版本庫內(nèi)容提交到遠(yuǎn)程的版本庫


  • cd 目錄 切換到需要使用git進(jìn)行版本控制的項(xiàng)目目錄中

  • 配置用戶名和郵箱

  1. git config --global user.name '用戶名'
  2. git config --global user.email '郵箱'
  • git 版本庫的初始化:創(chuàng)建版本庫
  1. git init
  • 將工作區(qū)的已經(jīng)修改過的文件,提交到本地版本庫中的暫存區(qū)
  1. git add . # 一次性全部提交完畢 add命令可將該文件添加到暫存區(qū)
  2. git status # 查看狀態(tài)
  • 在將所有的內(nèi)容從本地暫存區(qū)一次性的提交到本地的版本庫
  1. git commit -m '2020-09-10 22:02' //'注釋'
  • 添加遠(yuǎn)程版本庫
  1. git remote add origin https://gitee.com/bnkt/git-edu.git
  • 查看遠(yuǎn)程倉庫
  1. git remote -v
  • 提交到遠(yuǎn)程倉庫
  1. git push origin master //指定遠(yuǎn)程倉庫名和分支名
  2. git push -u origin master //將本地的master分支推送到origin主機(jī),同時(shí)指定origin為默認(rèn)主機(jī)
  3. git push -f origin master //本地強(qiáng)制上傳到遠(yuǎn)程,把遠(yuǎn)程的覆蓋
  • 從遠(yuǎn)程倉庫拉取內(nèi)容
  1. git pull https://gitee.com/jia-xiang/git-edu.git
  2. git pull origin master






  • 從遠(yuǎn)程倉庫拉取內(nèi)容




批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語:挺棒的, 原生路由理解了, 那么框架還難嗎?
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)