当前位置:首页 > VUE

vue实现户籍系统路由实现

2026-01-08 04:04:52VUE

户籍系统路由实现(Vue.js)

户籍系统通常需要管理居民信息、户籍变动、统计查询等功能模块。以下基于Vue Router的实现方案:

路由模块设计

src/router/index.js中定义核心路由结构:

import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/dashboard'
  },
  {
    path: '/dashboard',
    component: () => import('@/views/Dashboard.vue'),
    meta: { title: '系统首页' }
  },
  {
    path: '/resident',
    component: () => import('@/views/resident/Index.vue'),
    children: [
      {
        path: 'list',
        component: () => import('@/views/resident/List.vue'),
        meta: { title: '居民信息管理' }
      },
      {
        path: 'add',
        component: () => import('@/views/resident/Add.vue'),
        meta: { title: '新增居民' }
      }
    ]
  },
  {
    path: '/household',
    component: () => import('@/views/household/Index.vue'),
    children: [
      {
        path: 'transfer',
        component: () => import('@/views/household/Transfer.vue'),
        meta: { title: '户籍迁移' }
      },
      {
        path: 'split',
        component: () => import('@/views/household/Split.vue'),
        meta: { title: '分户处理' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to) => {
  document.title = to.meta.title || '户籍管理系统'
})

export default router

动态路由实现

对于需要权限控制的系统,可结合后端返回的权限数据动态生成路由:

// 在登录后调用此方法
function generateDynamicRoutes(userPermissions) {
  const dynamicRoutes = []

  if (userPermissions.includes('audit')) {
    dynamicRoutes.push({
      path: '/audit',
      component: () => import('@/views/Audit.vue'),
      meta: { title: '变更审核', requiresAuth: true }
    })
  }

  dynamicRoutes.forEach(route => {
    router.addRoute(route)
  })
}

路由守卫配置

添加全局路由守卫进行权限校验:

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')

  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isAuthenticated) {
      next({ path: '/login', query: { redirect: to.fullPath } })
    } else {
      next()
    }
  } else {
    next()
  }
})

嵌套路由视图

主布局文件中需放置顶层路由视图:

<!-- App.vue -->
<template>
  <div id="app">
    <main-header />
    <div class="main-container">
      <router-view />
    </div>
  </div>
</template>

子模块中使用二级路由视图:

<!-- views/resident/Index.vue -->
<template>
  <div class="resident-wrapper">
    <side-menu />
    <div class="content">
      <router-view />
    </div>
  </div>
</template>

路由传参方式

居民详情页可采用三种传参方式:

  1. 路径参数
    
    // 路由定义
    { path: '/resident/detail/:id', component: ResidentDetail }

// 组件获取 const route = useRoute() const residentId = route.params.id


2. 查询参数
```javascript
// 导航跳转
router.push({ path: '/resident/detail', query: { id: 123 } })

// 组件获取
const residentId = route.query.id
  1. 状态管理
    
    // 跳转前存储
    store.commit('setCurrentResident', residentData)

// 目标组件获取 const currentResident = computed(() => store.state.currentResident)

vue实现户籍系统路由实现

标签: 户籍路由
分享给朋友:

相关文章

php 实现路由

php 实现路由

PHP 实现路由的方法 在 PHP 中实现路由功能可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 PHP 实现简单路由 通过解析 URL 并匹配对应的处理逻辑,可以实现基本的路由功能。以…

vue路由实现模式

vue路由实现模式

Vue路由实现模式 Vue Router 提供了两种路由实现模式:Hash 模式和 History 模式。两种模式的主要区别在于 URL 的表现形式以及后端支持的需求。 Hash 模式 Hash 模…

vue实现tab路由

vue实现tab路由

Vue 实现 Tab 路由的方法 在 Vue 中实现 Tab 路由通常需要结合 Vue Router 和动态组件或条件渲染。以下是几种常见方法: 使用 Vue Router 动态路由 配置路由文件…

vue实现路由弹窗

vue实现路由弹窗

vue实现路由弹窗的方法 在Vue中实现路由弹窗可以通过多种方式完成,以下是几种常见的实现方法: 使用动态路由和组件 在路由配置中定义一个动态路由,用于渲染弹窗组件。这种方法适用于需要根据路由参数动…

vue历史路由实现

vue历史路由实现

Vue 历史路由实现 在 Vue 中实现历史路由通常依赖于 vue-router 库,并结合浏览器的 History API。以下是具体的实现方法: 安装 vue-router 确保项目中已安装 v…

vue实现动态路由

vue实现动态路由

动态路由的实现方式 Vue中实现动态路由通常涉及前端和后端的协作,以下是几种常见的实现方式: 基于用户权限的路由控制 在用户登录后,根据其权限动态生成路由表。使用router.addRoutes方…