当前位置:首页 > VUE

vue router怎么实现

2026-03-10 00:27:32VUE

Vue Router 实现方法

Vue Router 是 Vue.js 的官方路由管理器,用于构建单页应用(SPA)。以下是实现 Vue Router 的基本步骤和配置方法。

安装 Vue Router

通过 npm 或 yarn 安装 Vue Router:

npm install vue-router
# 或
yarn add vue-router

基本配置

在 Vue 项目中引入并配置 Vue Router:

import { createRouter, createWebHistory } from 'vue-router';
import Home from './views/Home.vue';
import About from './views/About.vue';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: Home },
    { path: '/about', component: About }
  ]
});

export default router;

路由视图和导航

在 Vue 应用的入口文件(如 main.js)中引入并挂载路由:

import { createApp } from 'vue';
import App from './App.vue';
import router from './router';

const app = createApp(App);
app.use(router);
app.mount('#app');

在模板中使用 <router-view><router-link>

<template>
  <div>
    <router-link to="/">Home</router-link>
    <router-link to="/about">About</router-link>
    <router-view></router-view>
  </div>
</template>

动态路由

通过冒号 : 定义动态路由参数:

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/user/:id', component: User }
  ]
});

在组件中通过 $route.params 访问参数:

export default {
  mounted() {
    console.log(this.$route.params.id);
  }
}

嵌套路由

通过 children 属性定义嵌套路由:

vue router怎么实现

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/user',
      component: User,
      children: [
        { path: 'profile', component: Profile },
        { path: 'posts', component: Posts }
      ]
    }
  ]
});

在父组件中使用 <router-view> 显示子路由:

<template>
  <div>
    <h1>User</h1>
    <router-view></router-view>
  </div>
</template>

路由守卫

通过路由守卫实现权限控制或导航逻辑:

router.beforeEach((to, from, next) => {
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

懒加载路由

使用动态导入实现路由懒加载:

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: () => import('./views/Home.vue') },
    { path: '/about', component: () => import('./views/About.vue') }
  ]
});

命名路由

通过 name 属性为路由命名:

vue router怎么实现

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'home', component: Home },
    { path: '/about', name: 'about', component: About }
  ]
});

通过名称导航:

<router-link :to="{ name: 'home' }">Home</router-link>

编程式导航

通过 router.pushrouter.replace 实现编程式导航:

this.$router.push('/about');
this.$router.push({ name: 'about' });
this.$router.replace('/login');

路由元信息

通过 meta 字段定义路由元信息:

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/admin', component: Admin, meta: { requiresAuth: true } }
  ]
});

滚动行为

通过 scrollBehavior 控制页面滚动行为:

const router = createRouter({
  history: createWebHistory(),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
  routes: [...]
});

路由模式

Vue Router 支持两种历史模式:

  • createWebHistory: HTML5 模式(无 #
  • createWebHashHistory: Hash 模式(带 #
import { createWebHashHistory } from 'vue-router';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...]
});

标签: vuerouter
分享给朋友:

相关文章

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

分页实现vue

分页实现vue

分页实现(Vue) 在Vue中实现分页功能通常需要结合前端分页逻辑和后端API支持。以下是两种常见的实现方式: 前端分页实现 适用于数据量较小的情况,直接在客户端完成分页逻辑。 <templ…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue搜索实现

vue搜索实现

Vue 搜索功能实现方法 基于计算属性的搜索 在 Vue 中可以通过计算属性实现简单的搜索功能。这种方式适合小型数据集,利用 JavaScript 的 filter 方法进行筛选。 data() {…