新手入门vue-router(一)


前言

我们的html、css、js等文件经webpack打包,存放在服务器中,当我们发起请求,每个url对应一个js文件,然后渲染出我们的页面。url与js文件的映射关系被称为路由映射表,这就是路由的基本定义。
vue-router为了单页面富应用(SPA)而生,利用vue-router,我们的页面实现了无刷新更改页面url,访问不同的内容。webpack打包时,会将每一个路由文件单独打包为一个js,在页面请求时按需加载。


一、vue-router的使用

上一篇博客讲了怎么使用vue-cli创建脚手架工程,这里同样使用CLI创建vue-router工程,工程会自带两个路由Home.vue、About.vue。总结vue-router使用步骤:

1、 创建将要使用的路由.vue文件;
在这里插入图片描述
这里about和home以及app、helloworld是脚手架工程自带的,实现两个简单的路由标签跳转。还有很多其他功能下面会一一介绍。
在这里插入图片描述

2、在router文件夹下的index.js中配置路由:

  1. 创建路由对象
  2. 配置路由routes映射关系
  3. 导出路由对象
    下面给出index.js的示例代码:
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
// 2、配置路由映射关系
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    // 嵌套子路由,在父路由Home.vue文件中决定显示
    children: [
      {
        path: '/',
        // 默认路径
        redirect: 'news'
      },
      {
        path: '/news',
        name: 'News',
        component: () => import('../components/HomeNews.vue')
      },
      {
        path: '/message',
        name: 'Message',
        component: () => import('../components/HomeMessage.vue')
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    // 动态路由 path: '/user/:userId'
    path: '/user/:userId',
    name: 'User',
    // 路由懒加载,打包时路由组件打包到单独的js文件中,用到时再加载,提升浏览器速度
    component: () => import('../components/User.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../components/Profile.vue')
  }
]
// 1、创建路由对象
const router = createRouter({
  routes,
  // 配置history路由
  history: createWebHistory(process.env.BASE_URL),
  
  // linkActiveClass属性修改被点击的router-link标签类名
  // 原始类为router-link-active、router-link-exact-active,修改只改变前者
  linkActiveClass: 'active'
})
// 3、导出路由
export default router

3、使用路由,并确定其显示位置


<template>
   <div id="nav">
       <!--router的全局组件,最终渲染成<a>标签-->
       <!--tag\replace属性无效??-->
       <router-link to="/">Home</router-link> |
       <router-link to="/about">About</router-link> |
       <button @click="aboutClick">About</button> |
       <router-link :to="'/user/' +  userId">User</router-link> |
       <!--如果想用button来跳转,同样使用this.$router.push({path: '/profile', query: {name: 'zkl', age: 23, height: 173.5}})-->
       <router-link :to="{path: '/profile', query: {name: 'zkl', age: 23, height: 173.5}}">Profile</router-link>
       <div id="app">
           <!--router-view决定router映射的组件显示位置,这里就是home、about下方-->
           <router-view></router-view>
       </div>
   </div>

</template>

<script>
export default {
 name: 'App',
 data() {
   return {
     userId: 'zkl',
   };
 },
 methods: {
   aboutClick() {
     // this.$router获取路由对象
     this.$router.push('/about');
   },
 },
};
</script>

<style scoped>
#app {
 font-family: Avenir, Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
}

#nav {
 padding: 30px;
 text-align: center;
}

#nav a {
 font-weight: bold;
 color: #2c3e50;
}

#nav a.active {
 color: #42b983;
}
</style>

二、嵌套子路由

使用children属性,在配置路由映射关系时配置嵌套子路由。上面的index.js已经给出了配置代码,子路由要在其父路由中设置路由标签和显示位置。这里news、message都是home的嵌套路由。

代码如下(示例):

<template>
    <div class="home">
        <router-link to="/news">News</router-link> |
        <router-link to="/message">Message</router-link>
        <router-view></router-view>
        <br>
        <img alt="Vue logo"
            src="../assets/logo.png">
        <HelloWorld :msg="msg" />
    </div>
</template>

<script>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue';

export default {
  name: 'Home',
  data() {
    return {
      msg: 'Welcome to Your Vue.js App',
    };
  },
  components: {
    HelloWorld,
  },
};
</script>

三. 动态路由

动态路由的url路径不是写死的,而是动态变化的。例如user,我们访问某一用户的相关信息时,通常以这样的路径/user/userid,那么后面的userid就是动态变化的,不能写死。

配置动态路由使用 path: '/user/:userId’这种方式,使用时 User动态绑定userId。
代码如下(示例):

import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
// 2、配置路由映射关系
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    // 嵌套子路由,在父路由Home.vue文件中决定显示
    children: [
      {
        path: '/',
        // 默认路径
        redirect: 'news'
      },
      {
        path: '/news',
        name: 'News',
        component: () => import('../components/HomeNews.vue')
      },
      {
        path: '/message',
        name: 'Message',
        component: () => import('../components/HomeMessage.vue')
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    // 动态路由 path: '/user/:userId'
    path: '/user/:userId',
    name: 'User',
    // 路由懒加载,打包时路由组件打包到单独的js文件中,用到时再加载,提升浏览器速度
    component: () => import('../components/User.vue')
  },
  {
    path: '/profile',
    name: 'Profile',
    component: () => import('../components/Profile.vue')
  }
]
// 1、创建路由对象
const router = createRouter({
  routes,
  // 配置history路由
  history: createWebHistory(process.env.BASE_URL),
  
  // linkActiveClass属性修改被点击的router-link标签类名
  // 原始类为router-link-active、router-link-exact-active,修改只改变前者
  linkActiveClass: 'active'
})
// 3、导出路由
export default router

可以通过this. r o u t e . p a r a m s . u s e r I d 来 访 问 参 数 。 t h i s . route.params.userId来访问参数。this. route.params.userId访this.route获取到当前活跃路由。this.$router获取整个路由对象。注意区分

<template>
    <div>
        <h2>我是用户组件,我是一个动态路由</h2>
        <p>我是用户的相关信息</p>
        <!--$toute获取当前活跃路由-->
        <h2>{{$route.params.userId}}</h2>
    </div>
</template>

该处使用的url网络请求的数据。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的体育馆管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本体育馆管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此体育馆管理系统利用当下成熟完善的SpringBoot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线选择试题并完成答题,在线查看考核分数。管理员管理收货地址管理、购物车管理、场地管理、场地订单管理、字典管理、赛事管理、赛事收藏管理、赛事评价管理、赛事订单管理、商品管理、商品收藏管理、商品评价管理、商品订单管理、用户管理、管理员管理等功能。体育馆管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:体育馆管理系统;SpringBoot框架;Mysql;自动化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值