当前位置:首页 > VUE

js实现vue-router

2026-02-24 04:47:17VUE

实现基本路由功能

在JavaScript中实现类似vue-router的功能,可以通过监听URL变化并动态渲染对应组件完成。以下是核心实现方式:

class VueRouter {
  constructor(options) {
    this.routes = options.routes || [];
    this.current = window.location.pathname;
    window.addEventListener('popstate', this.handlePopState.bind(this));
  }

  handlePopState() {
    this.current = window.location.pathname;
    this.render();
  }

  push(path) {
    window.history.pushState({}, '', path);
    this.current = path;
    this.render();
  }

  render() {
    const route = this.routes.find(r => r.path === this.current);
    if (route) {
      document.getElementById('app').innerHTML = route.component;
    }
  }
}

路由映射配置

需要定义路由与组件的映射关系,这是路由系统的核心配置:

js实现vue-router

const routes = [
  { path: '/', component: '<div>Home Page</div>' },
  { path: '/about', component: '<div>About Page</div>' },
  { path: '/contact', component: '<div>Contact Page</div>' }
];

const router = new VueRouter({
  routes
});

HTML5 History API集成

现代前端路由通常使用HTML5 History API来实现无刷新跳转:

// 导航方法
function navigate(e) {
  e.preventDefault();
  const path = e.target.getAttribute('href');
  router.push(path);
}

// 为链接添加事件监听
document.querySelectorAll('a[href]').forEach(link => {
  link.addEventListener('click', navigate);
});

动态路由匹配

实现类似vue-router的动态路由参数功能:

js实现vue-router

class VueRouter {
  // ...其他代码

  matchRoute(path) {
    return this.routes.find(route => {
      const routeSegments = route.path.split('/');
      const pathSegments = path.split('/');

      if (routeSegments.length !== pathSegments.length) {
        return false;
      }

      return routeSegments.every((seg, i) => {
        return seg.startsWith(':') || seg === pathSegments[i];
      });
    });
  }

  render() {
    const route = this.matchRoute(this.current);
    if (route) {
      const params = this.extractParams(route.path, this.current);
      document.getElementById('app').innerHTML = route.component(params);
    }
  }

  extractParams(routePath, currentPath) {
    const params = {};
    const routeParts = routePath.split('/');
    const pathParts = currentPath.split('/');

    routeParts.forEach((part, i) => {
      if (part.startsWith(':')) {
        params[part.slice(1)] = pathParts[i];
      }
    });

    return params;
  }
}

路由守卫实现

添加类似vue-router的导航守卫功能:

class VueRouter {
  constructor(options) {
    this.beforeEachHooks = [];
    // ...其他初始化代码
  }

  beforeEach(fn) {
    this.beforeEachHooks.push(fn);
  }

  push(path) {
    const from = this.current;
    const to = path;

    this.runHooks(from, to, () => {
      window.history.pushState({}, '', path);
      this.current = path;
      this.render();
    });
  }

  runHooks(from, to, next) {
    let index = 0;

    const nextHook = () => {
      if (index >= this.beforeEachHooks.length) {
        return next();
      }

      const hook = this.beforeEachHooks[index++];
      hook(from, to, nextHook);
    };

    nextHook();
  }
}

嵌套路由支持

实现嵌套路由功能需要递归匹配路由:

class VueRouter {
  // ...其他代码

  matchRoute(path, routes = this.routes) {
    for (const route of routes) {
      if (route.path === path) {
        return route;
      }

      if (route.children) {
        const childPath = path.replace(route.path, '');
        const matched = this.matchRoute(childPath, route.children);
        if (matched) {
          return {
            ...route,
            child: matched
          };
        }
      }
    }
    return null;
  }

  render() {
    const route = this.matchRoute(this.current);
    if (route) {
      let content = route.component;

      if (route.child) {
        const outlet = document.createElement('div');
        outlet.id = 'router-outlet';
        content += outlet.outerHTML;
      }

      document.getElementById('app').innerHTML = content;

      if (route.child) {
        document.getElementById('router-outlet').innerHTML = route.child.component;
      }
    }
  }
}

这些实现方式展示了如何用纯JavaScript构建一个基本的路由系统,包含了vue-router的核心功能。实际应用中可能需要考虑更多边界情况和性能优化。

标签: jsvue
分享给朋友:

相关文章

vue实现好友列表

vue实现好友列表

实现好友列表的Vue组件 数据准备 在Vue组件的data中定义好友列表数据,通常是一个数组对象,每个对象包含好友的基本信息如ID、头像、昵称等。 data() { return { f…

vue实现用户信息删除

vue实现用户信息删除

Vue 实现用户信息删除功能 在 Vue 中实现用户信息删除功能通常涉及前端界面交互和后端 API 调用。以下是实现步骤: 前端界面设计 创建删除按钮或操作项,通常使用 @click 事件触发删除操…

vue前端实现下载进度

vue前端实现下载进度

实现下载进度条的基本思路 在Vue中实现下载进度条,通常需要结合XMLHttpRequest或Fetch API来监听下载进度事件。通过计算已下载数据与总数据的比例,动态更新进度条的显示。 使用XM…

vue全选实现

vue全选实现

Vue 全选实现方法 在 Vue 中实现全选功能通常涉及数据绑定和事件处理。以下是几种常见的实现方式: 基础实现方式 <template> <div> <…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue列表实现

vue列表实现

Vue 列表实现方法 使用 v-for 指令 v-for 是 Vue 中用于渲染列表的核心指令,基于数据源动态生成 DOM 元素。语法格式为 item in items 或 (item, index)…