当前位置:首页 > 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;
    }
  }
}

路由映射配置

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

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的动态路由参数功能:

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的核心功能。实际应用中可能需要考虑更多边界情况和性能优化。

js实现vue-router

标签: jsvue
分享给朋友:

相关文章

vue卖座网实现

vue卖座网实现

Vue 卖座网实现 项目结构搭建 使用 Vue CLI 快速初始化项目,安装必要依赖如 Vue Router、Vuex、Axios。 创建核心目录结构:components(通用组件)、views(页…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现定时

vue实现定时

Vue 实现定时功能的方法 使用 setInterval 和 clearInterval 在 Vue 中可以通过 setInterval 和 clearInterval 实现定时功能。在组件的 mou…

vue实现tap

vue实现tap

Vue 实现 Tap 事件 在移动端开发中,Tap 事件比 Click 事件更常用,因为 Click 事件有 300ms 延迟。以下是几种在 Vue 中实现 Tap 事件的方法。 使用第三方库 安装…

vue实现datalist

vue实现datalist

使用 Vue 实现 HTML5 的 datalist 功能 HTML5 的 <datalist> 元素提供了一种自动完成输入框的功能,允许用户在输入时从预定义的选项列表中选择。以下是几种在…