Blogger Information
Blog 47
fans 3
comment 0
visits 38122
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
哈希锚点原生实现一个路由、使用vue路由模块实现一个前端路由
Original
634 people have browsed it

1.哈希锚点实现一个原生的路由

  1. <nav>
  2. <a href="#list1">国内新闻</a>
  3. <a href="#list2">娱乐新闻</a></a>
  4. </nav>
  5. <!-- 该区域用于显示路由的内容 -->
  6. <div class="route-view"></div>
  7. <script>
  8. let list1 = `
  9. <ul>
  10. <li><a href="">返乡人员如何划定?国家卫健委最新回应</a></li>
  11. <li><a href="">返乡人员如何划定?国家卫健委最新回应</a></li>
  12. <li><a href="">返乡人员如何划定?国家卫健委最新回应</a></li>
  13. </ul>
  14. `;
  15. let list2 = `
  16. <ul>
  17. <li><a href="">都当妈了还不许人家晒晒娃?</a></li>
  18. <li><a href="">都当妈了还不许人家晒晒娃?</a></li>
  19. <li><a href="">都当妈了还不许人家晒晒娃?</a></li>
  20. </ul>
  21. `;
  22. // 获取路由的内容显示区元素
  23. const routeView = document.querySelector(".route-view");
  24. window.addEventListener("hashchange", show);
  25. // 页面加载完就执行
  26. window.addEventListener("DOMContentLoaded", show);
  27. function show() {
  28. // console.log(location.hash);
  29. switch (location.hash) {
  30. case "#list1":
  31. routeView.innerHTML = list1;
  32. return;
  33. case "#list2":
  34. routeView.innerHTML = list2;
  35. return;
  36. default:
  37. routeView.innerHTML = list1;
  38. }
  39. }
  40. </script>

2.vue路由模块实现一个前端路由

  • 点击今日话题

  • 点击游戏新闻

  1. <!-- 加载vue框架 -->
  2. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  3. <!-- 路由模块 -->
  4. <script src="vue-router-dev/dist/vue-router.js"></script>
  5. <body>
  6. <nav class="app">
  7. <!-- vue路由锚点 -->
  8. <router-link to="/list1">今日话题</router-link>
  9. <router-link to="/list2">游戏新闻</router-link>
  10. <!-- 路由到的资源显示区域 -->
  11. <router-view></router-view>
  12. </nav>
  13. <script>
  14. const list1 = {
  15. template: `
  16. <ul>
  17. <li><a href="">80多份裁判文书背后的中老年“养生培训”套路</a></li>
  18. <li><a href="">80多份裁判文书背后的中老年“养生培训”套路</a></li>
  19. <li><a href="">80多份裁判文书背后的中老年“养生培训”套路</a></li>
  20. </ul>
  21. `,
  22. };
  23. const list2 = {
  24. template: `
  25. <ul>
  26. <li><a href="">LPL春季赛:RNG2-0击败OMGCryin豪取5杀</a></li>
  27. <li><a href="">LPL春季赛:RNG2-0击败OMGCryin豪取5杀</a></li>
  28. <li><a href="">LPL春季赛:RNG2-0击败OMGCryin豪取5杀</a></li>
  29. </ul>
  30. `,
  31. };
  32. // 1. 创建路由
  33. const router = new VueRouter({
  34. // 配置路由
  35. routes: [
  36. { path: "/list1", component: list1 },
  37. { path: "/list2", component: list2 },
  38. ],
  39. });
  40. new Vue({
  41. // 2. 注册路由
  42. router,
  43. }).$mount(".app");
  44. </script>
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments