• vue2学习之前端路由小案例(后台管理小案例)


    目录

    前端路由小案例(后台管理小案例)

    1 项目整体功能演示

    3 主要代码


    前端路由小案例(后台管理小案例)

    1 项目整体功能演示

    进入网站自动进入登录页面:

     

     登录成功自动存储token,然后跳转到登录成功页面:

     

     左边功能栏可以切换:

     

     点击用户管理的操作可以跳转到用户详情页面,并却可以回退到上一步:

     

    完了之后可以退出登录,并清除token,然后跳转到登录页面。

    2 案例用到的知识点

    • 命名路由

    • 路由重定向

    • 导航守卫

    • 嵌套路由

    • 动态路由匹配

    • 编程式导航

     项目完整源码下载:

    1. gitee:https://gitee.com/xingyueqianduan/qianduanroutericon-default.png?t=M666https://gitee.com/xingyueqianduan/qianduanrouter

     2.github:GitHub - 858399075/vue-routerdemo: 前端路由小案例-后台管理系统案例。案例用到的知识点:命名路由,路由重定向,导航守卫,嵌套路由,动态路由匹配,编程式导航。用来复习前端路由。前端路由小案例-后台管理系统案例。案例用到的知识点:命名路由,路由重定向,导航守卫,嵌套路由,动态路由匹配,编程式导航。用来复习前端路由。 - GitHub - 858399075/vue-routerdemo: 前端路由小案例-后台管理系统案例。案例用到的知识点:命名路由,路由重定向,导航守卫,嵌套路由,动态路由匹配,编程式导航。用来复习前端路由。https://github.com/858399075/vue-routerdemo

    3 主要代码

    3.1 app.vue

    1. <template>
    2.  <router-view>router-view>
    3. template>
    4. <script>
    5. export default {
    6.  name: 'MyApp',
    7. }
    8. script>
    9. <style lang="less" scoped>
    10. style>

    3.2 MyHome.vue

    1. <template>
    2.  <div class="home-container">
    3.    
    4.    <MyHeader>MyHeader>
    5.    
    6.    <div class="home-main-box">
    7.      
    8.      <MyAside>MyAside>
    9.      
    10.      <div class="home-main-body">
    11.        <router-view>router-view>
    12.      div>
    13.    div>
    14.  div>
    15. template>
    16. <script>
    17. // 头部区域组件
    18. import MyHeader from './subcomponents/MyHeader.vue'
    19. // 左侧边栏组件
    20. import MyAside from './subcomponents/MyAside.vue'
    21. export default {
    22.  name: 'MyHome',
    23.  // 注册组件
    24.  components: {
    25.    MyHeader,
    26.    MyAside,
    27. },
    28. }
    29. script>
    30. <style lang="less" scoped>
    31. .home-container {
    32.  height: 100%;
    33.  display: flex;
    34.  flex-direction: column;
    35.  .home-main-box {
    36.    height: 100%;
    37.    display: flex;
    38.    .home-main-body {
    39.      padding: 15px;
    40.      flex: 1;
    41.   }
    42. }
    43. }
    44. style>

    3.3 MyLogin.vue

    1. <template>
    2.  <div class="login-container">
    3.    <div class="login-box">
    4.      
    5.      <div class="text-center avatar-box">
    6.        <img src="../assets/logo.png" class="img-thumbnail avatar" alt="">
    7.      div>
    8.      
    9.      <div class="form-login p-4">
    10.        
    11.        <div class="form-group form-inline">
    12.          <label for="username">登录名称label>
    13.          <input type="text" class="form-control ml-2" id="username" placeholder="请输入登录名称" v-model.trim="username" autocomplete="off">
    14.        div>
    15.        
    16.        <div class="form-group form-inline">
    17.          <label for="password">登录密码label>
    18.          <input type="password" class="form-control ml-2" id="password" placeholder="请输入登录密码" v-model.trim="password">
    19.        div>
    20.        
    21.        <div class="form-group form-inline d-flex justify-content-end">
    22.          <button type="button" class="btn btn-secondary mr-2" @click="reset">重置button>
    23.          <button type="button" class="btn btn-primary" @click="login">登录button>
    24.        div>
    25.      div>
    26.    div>
    27.  div>
    28. template>
    29. <script>
    30. export default {
    31.  name: 'MyLogin',
    32.  data(){
    33.    return{
    34.      username:'',
    35.      password:''
    36.   }
    37. },
    38.  methods:{
    39.    reset(){
    40.      this.username = '',
    41.      this.password = ''
    42.   },
    43.    login(){
    44.      if(this.username === 'admin' && this.password === '666666'){
    45.        //登录成功
    46.        //1.存储token
    47.        //2.跳转到后台主页
    48.        localStorage.setItem('token','Bearer xxx')
    49.        this.$router.push('/home')
    50.     }else{
    51.        //登录失败
    52.        localStorage.removeItem('token')
    53.        alert('登录失败!')
    54.        this.username = ''
    55.        this.password = ''
    56.     }
    57.   }
    58. }
    59. }
    60. script>
    61. <style lang="less" scoped>
    62. .login-container {
    63.  background-color: #35495e;
    64.  height: 100%;
    65.  .login-box {
    66.    width: 400px;
    67.    height: 250px;
    68.    background-color: #fff;
    69.    border-radius: 3px;
    70.    position: absolute;
    71.    left: 50%;
    72.    top: 50%;
    73.    transform: translate(-50%, -50%);
    74.    box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
    75.    .form-login {
    76.      position: absolute;
    77.      bottom: 0;
    78.      left: 0;
    79.      width: 100%;
    80.      box-sizing: border-box;
    81.   }
    82. }
    83. }
    84. .form-control {
    85.  flex: 1;
    86. }
    87. .avatar-box {
    88.  position: absolute;
    89.  width: 100%;
    90.  top: -65px;
    91.  left: 0;
    92.  .avatar {
    93.    width: 120px;
    94.    height: 120px;
    95.    border-radius: 50% !important;
    96.    box-shadow: 0 0 6px #efefef;
    97. }
    98. }
    99. style>

    3.4 MyAside.vue

    1. <template>
    2.  <div class="layout-aside-container">
    3.    
    4.    <ul class="user-select-none menu">
    5.      <li class="menu-item">
    6.        <router-link to="/home/users">用户管理router-link>
    7.      li>
    8.      <li class="menu-item">
    9.        <router-link to="/home/rights">权限管理router-link>
    10.      li>
    11.      <li class="menu-item">
    12.        <router-link to="/home/goods">商品管理router-link>
    13.      li>
    14.      <li class="menu-item">
    15.        <router-link to="orders">订单管理router-link>
    16.      li>
    17.      <li class="menu-item">
    18.        <router-link to="setting">系统设置router-link>
    19.      li>
    20.    ul>
    21.  div>
    22. template>
    23. <script>
    24. export default {
    25.  name: 'MyAside',
    26. }
    27. script>
    28. <style lang="less" scoped>
    29. .layout-aside-container {
    30.  width: 250px;
    31.  height: 100%;
    32.  border-right: 1px solid #eaeaea;
    33. }
    34. .menu {
    35.  list-style-type: none;
    36.  padding: 0;
    37.  .menu-item {
    38.    line-height: 50px;
    39.    font-weight: bold;
    40.    font-size: 14px;
    41.    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    42.      Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    43.   &:hover {
    44.      background-color: #efefef;
    45.      cursor: pointer;
    46.   }
    47.    a {
    48.      display: block;
    49.      color: black;
    50.      padding-left: 30px;
    51.     &:hover {
    52.        text-decoration: none;
    53.     }
    54.   }
    55. }
    56. }
    57. // 设置路由高亮效果
    58. .router-link-active {
    59.  background-color: #efefef;
    60.  box-sizing: border-box;
    61.  position: relative;
    62. // 伪元素实现路由高亮效果
    63. &::before {
    64.    content: ' ';
    65.    display: block;
    66.    width: 4px;
    67.    height: 100%;
    68.    position: absolute;
    69.    left: 0;
    70.    top: 0;
    71.    background-color: #42b983;
    72. }
    73. }
    74. style>

    3.5 MyHeader.vue

    1. <template>
    2.  <div class="layout-header-container d-flex justify-content-between align-items-center p-3">
    3.    
    4.    <div class="layout-header-left d-flex align-items-center user-select-none">
    5.      
    6.      
    7.      
    8.      <h4 class="layout-header-left-title ml-3">星月后台管理系统h4>
    9.    div>
    10.    
    11.    <div class="layout-header-right">
    12.      <button type="button" class="btn btn-light" @click="logout">退出登录button>
    13.    div>
    14.  div>
    15. template>
    16. <script>
    17. export default {
    18.  name: 'MyHeader',
    19.  methods:{
    20.    logout(){
    21.      //1.清除token
    22.      //2.跳转到登录页
    23.      localStorage.removeItem('token')
    24.      this.$router.push('/login')
    25.   }
    26. }
    27. }
    28. script>
    29. <style lang="less" scoped>
    30. .layout-header-container {
    31.  height: 60px;
    32.  border-bottom: 1px solid #eaeaea;
    33. }
    34. .layout-header-left-img {
    35.  height: 50px;
    36. }
    37. style>

    3.6 MyUsers.vue

    1. <template>
    2.  <div>
    3.    
    4.    <h4 class="text-center">用户管理h4>
    5.    
    6.    <table class="table table-bordered table-striped table-hover">
    7.      <thead>
    8.        <tr>
    9.          <th>#th>
    10.          <th>姓名th>
    11.          <th>年龄th>
    12.          <th>头衔th>
    13.          <th>操作th>
    14.        tr>
    15.      thead>
    16.      <tbody>
    17.        <tr v-for="item in userlist" :key="item.id">
    18.          <td>{{ item.id }}td>
    19.          <td>{{ item.name }}td>
    20.          <td>{{ item.age }}td>
    21.          <td>{{ item.position }}td>
    22.          <td>
    23.            <a href="#" @click.prevent="gotoDetail(item.id)">详情a>
    24.          td>
    25.        tr>
    26.      tbody>
    27.    table>
    28.  div>
    29. template>
    30. <script>
    31. export default {
    32.  name: 'MyUser',
    33.  data() {
    34.    return {
    35.      // 用户列表数据
    36.      userlist: [
    37.       { id: 1, name: '张三', age: 18, position: '快递员' },
    38.       { id: 2, name: '张四', age: 18, position: '外卖员' },
    39.       { id: 3, name: '张五', age: 19, position: 'Boss' },
    40.       { id: 4, name: '老六', age: 22, position: '老六' }
    41.     ]
    42.   }
    43. },
    44.  methods:{
    45.    gotoDetail(id){
    46.      // console.log('ok')
    47.      this.$router.push('/home/userinfo/' + id )
    48.      console.log(this);
    49.   }
    50. }
    51. }
    52. script>
    53. <style lang="less" scoped>style>
    ​3.7 router/index.js (路由模块代码)
    1. import Vue from 'vue'
    2. import VueRouter from 'vue-router'
    3. //导入前置路由地址数组
    4. import Arrrouter from '@/router/router.js'
    5. import Login from '@/components/MyLogin.vue'
    6. import Home from '@/components/MyHome.vue'
    7. import Users from '@/components/menus/MyUsers.vue'
    8. import Rights from '@/components/menus/MyRights.vue'
    9. import Goods from '@/components/menus/MyGoods.vue'
    10. import Orders from '@/components/menus/MyOrders.vue'
    11. import Setting from '@/components/menus/MySettings.vue'
    12. import UserDetail from '@/components/user/MyUserDetail.vue'
    13. Vue.use(VueRouter)
    14. const router = new VueRouter({
    15.  routes:[
    16.    //路由重定向
    17.   { path:'/',redirect:'/login'},
    18.    //登录路由规则
    19.   { path:'/login',component:Login},
    20.   {
    21.      path:'/home',
    22.      component:Home,
    23.      //路由重定向
    24.      redirect:'/home/users',
    25.      children:[
    26.        //路由重定向
    27.        // { path:'/',redirect:'users' },
    28.        //子路由规则
    29.       { path:'users',component:Users },
    30.       { path:'rights',component:Rights },
    31.       { path:'goods',component:Goods },
    32.       { path:'orders',component:Orders },
    33.       { path:'setting',component:Setting },
    34.        //用户详情页路由规则
    35.       { path:'userinfo/:id',component:UserDetail,props:true }
    36.   ]
    37.   },
    38. ]
    39. })
    40. // 全局前置守卫
    41. router.beforeEach((to,from,next)=>{
    42.  if(Arrrouter.indexOf(to.path) != -1){
    43.    const token = localStorage.getItem('token')
    44.    if(token){
    45.      next()
    46.   }else{
    47.      next('/login')
    48.   }
    49. }else{
    50.    next()
    51. }
    52. })
    53. export default router

    3.8 router (全局守卫路由路径)

    export default ['/home','/home/users','/home/orders','/home/rights','/home/setting']

    星月前端博客https://xingyue.vercel.app/

    个人博客,记录前端学习笔记,欢迎收藏或者提意见。

     

  • 相关阅读:
    JUC下的异步编程工具使用详情以及源码分析(FutureTask、CompletableFuture)
    LeetCode216. Combination Sum III
    【深度学习】深入解码:提升NLP生成文本的策略与参数详解
    利用vue模拟element-ui的分页器效果
    java数据结构与算法刷题-----LeetCode108:将有序数组转换为二叉搜索树
    编译原理习题两则(龙书,写出语言的正则定义)
    每天20题吃透这份将近 500 页的“Java 工程师八股文”,成功入职阿里
    ae如何去除视频水印?分享三个简单的方法!
    从零入门AI生图原理(一)(Datawhale X 魔搭 AI夏令营)
    if else 替换方案
  • 原文地址:https://blog.csdn.net/qq_61950936/article/details/126185348