当前位置:首页 > 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实现户籍系统路由实现

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

相关文章

vue路由实现

vue路由实现

Vue 路由实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的基本实现步骤和核心功能。 安装 Vue Router 通过…

vue实现路由

vue实现路由

Vue 路由的实现方法 Vue 路由可以通过 Vue Router 库来实现,Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue Router 通…

vue路由实现介绍

vue路由实现介绍

Vue 路由实现介绍 Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。它通过动态映射组件到路由实现页面无刷新切换,同时支持嵌套路由、路由参数、导航守卫等高级功能…

vue路由实现思路

vue路由实现思路

Vue 路由实现思路 Vue 路由的核心是通过 Vue Router 库实现的单页面应用(SPA)路由管理。以下是关键实现思路和步骤: 安装 Vue Router 通过 npm 或 yarn 安装…

Vue实现路由监

Vue实现路由监

Vue实现路由监听的方法 在Vue中,可以通过多种方式监听路由变化,以下是常见的实现方法: 使用Vue Router的全局前置守卫 router.beforeEach((to, from, n…