实现简易前端路由
发布日期:2021-05-10 19:07:40 浏览次数:9 分类:精选文章

本文共 2283 字,大约阅读时间需要 7 分钟。

������������������������������������������������������������������������������������������������

���������������������������������

������������������������������������������������������������ Web ������������������������������������������������������������������������������������������������������������������������������������������������������������������ Vue.js ��������������������������� Vue ������������������������������������������������������������������������

������������

���������������������������������������������

  • ������������������ ��� Vue ��������������������������������������������� history ���������������������
  • ������������������ ������������������������ UI ������������������������ Vue ������������
  • ������������������ ������ URL ��������� hash ���������������������������������������
  • ������������������������ URL ������������������������������������������
  • ������������������

  • ������������������������ history ������������������������������������
  • npm install history
    1. ������ Vue ������������������ Vue ������������������������
    2. const vm = new Vue({  el: '#app',  History: History})
      1. ���������������������������������������������������������������������������
      2. // home ������������const home = {  template: '
        ������������
        '}// tech ������������const tech = { template: '
        ������������
        '}
        1. ��������������������� Vue ������������������������������������
        2. const routes = {  '/': home,  '/tech': tech,  // ������������������}
          1. ������������������������������ hashchange ������������������������������
          2. window.addEventListener('hashchange', () => {  // ������������ URL ������������������  const currentPath = window.location.pathname  const routeComponent = routes[currentPath.replace('/','')]  vm.currentComponent = routeComponent})

            ������������

            • ������������ ������ hash ���������������������������������������������������������
            • ������������ ��� URL ������������������������������������������������������
            • ������������ ���������������������������������������������������������������������������������

            ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:vue-router的基本使用
    下一篇:前端路由||路由的基本概念与原理||在开发中,路由分为:  后端路由  前端路由

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月18日 05时35分49秒