• 前端基础(Vue Router路由的使用)


    前言:很多网站都有页面的跳转,那具体页面跳转是怎样实现的?今天学习前端SPA(Single page Application)单页面应用,不反复请求后端资源,而是通过路由实现页面的跳转。

    目录

    路由的创建

    main.ts导入路由

    App.vue文件

    展示效果

    子路由

    router文件导入

    子路由创建

    链接式

    编程式

    页面的跳转

    展示效果

    获取路由信息

    路由传参方式

    参数传参

    路由传参 

    完整代码


    安装vue-router

    npm install vue-router

    路由的创建

    在router文件夹下创建一个index.ts文件,默认导出

    路由的动态导入

    1. import { createRouter, createWebHistory } from 'vue-router'
    2. const router = createRouter({
    3. history: createWebHistory(),
    4. routes: [
    5. {
    6. path: "/home",
    7. component: () => {
    8. return import("@/views/Home.vue");
    9. },
    10. },
    11. {
    12. path: "/test",
    13. component: () => {
    14. return import('../views/Test.vue');
    15. },
    16. },
    17. ]
    18. })
    19. export default router;

    其中Home.vue文件

    1. <script setup>
    2. script>
    3. <style lang="scss" scoped>
    4. style>

    Test.vue文件 

    1. <script setup>
    2. script>
    3. <style lang="scss" scoped>
    4. style>

    main.ts导入路由

    在main.ts文件中导入使用,一定要保存!!!

    1. import router from './router'
    2. createApp(App).use(router).mount('#app')

     

    App.vue文件

    1. <script setup>
    2. script>
    3. <style scoped>
    4. style>

    展示效果

    子路由

    router文件导入

    1. import { createRouter, createWebHistory } from 'vue-router'
    2. const router = createRouter({
    3. history: createWebHistory(),
    4. routes: [
    5. {
    6. path: '/home',
    7. component: () => {
    8. return import('@/views/Home.vue')
    9. }
    10. },
    11. {
    12. path: '/test',
    13. component: () => import('../views/Info/Test.vue'),
    14. children: [
    15. {
    16. path: 'step',
    17. component: () => import('../views/Info/step.vue')
    18. },
    19. {
    20. path: 'result',
    21. component: () => import('../views/Info/result.vue')
    22. }
    23. ]
    24. }
    25. ]
    26. })
    27. export default router

    子路由创建

    链接式

    使用RouterLink元素 

    1. <script setup>
    2. script>
    3. <style lang="scss" scoped>
    4. style>

    编程式

    1. <script setup>
    2. // import router from '@/router'
    3. import { useRouter } from 'vue-router'
    4. const router = useRouter()
    5. function toStep() {
    6. router.push('test/step')
    7. }
    8. function toResult() {
    9. router.push('test/Result')
    10. }
    11. script>
    12. <style lang="scss" scoped>
    13. style>

    页面的跳转

    1. function backwordRouter() {
    2. router.back()
    3. }
    4. function forwordRouter() {
    5. router.forward()
    6. }
    7. function jumpRouter() {
    8. router.go(-3)
    9. }

    展示效果

    获取路由信息

    useRoute() 当前页面的路由信息

    useRouter() 整个项目的路由信息

    路由传参方式

    参数传参

    query urlencoded参数传参 键=值&键=值

  • <router-link to="/test/step?name=mrjj&stage=10">通过routerLink传参调用测试过程router-link>
  • 通过query调用

  • UI测试:{{ route.query.name }}
  • 接口测试:{{ route.query.stage }}
  • 路由传参 

  • <router-link to="/test/step/mrjj/100">通过Params传参router-link>
  • 通过params调用 

  • UI测试:{{ route.params.name }}
  • 接口测试:{{ route.params.stage }}
  • 通过props接受参数

    1. {path: 'result/:result1?/:result2?',
    2. props:true,
    3. component: () => import('../views/info/result.vue')}
  • <router-link to="/test/result/success/fail">result通过Params传参router-link>
    1. <script setup>
    2. const props = defineProps(['result1', 'result2'])
    3. script>
    4. <style lang="scss" scoped>
    5. style>

    完整代码

    router/index.ts

    1. import { createRouter, createWebHistory } from 'vue-router'
    2. const router = createRouter({
    3. history: createWebHistory(),
    4. routes: [
    5. {
    6. path: '/home',
    7. component: () => {
    8. return import('@/views/Home.vue')
    9. }
    10. },
    11. {
    12. path: '/test',
    13. component: () => import('../views/info/Test.vue'),
    14. children: [
    15. {
    16. path: 'step/:name?/:stage?',
    17. component: () => import('../views/info/step.vue')
    18. },
    19. {
    20. path: 'result/:result1?/:result2?',
    21. props:true,
    22. component: () => import('../views/info/result.vue')
    23. }
    24. ]
    25. }
    26. ]
    27. })
    28. export default router

    views/Test.vue 

    1. <script setup>
    2. // import router from '@/router'
    3. import { useRouter } from 'vue-router'
    4. const router = useRouter()
    5. function toStep() {
    6. router.push('test/step')
    7. }
    8. function toResult() {
    9. router.push('test/Result')
    10. }
    11. function backwordRouter() {
    12. router.back()
    13. }
    14. function forwordRouter() {
    15. router.forward()
    16. }
    17. function jumpRouter() {
    18. router.go(-3)
    19. }
    20. script>
    21. <style lang="scss" scoped>
    22. style>

    views/Step.vue

    1. <script setup>
    2. import { useRouter, useRoute } from 'vue-router'
    3. const router = useRouter()
    4. const route = useRoute()
    5. console.log('router是:', router)
    6. console.log('route是:', route)
    7. script>
    8. <style lang="scss" scoped>
    9. style>

    views/Result.vue

    1. <script setup>
    2. const props = defineProps(['result1', 'result2'])
    3. script>
    4. <style lang="scss" scoped>
    5. style>
  • 相关阅读:
    【SpringBoot】SpringBoot自动配置底层源码解析
    MQTT 基础--MQTT 发布、订阅和取消订阅 :第 4 部分
    JUC并发编程第六篇,带你了解Java内存模型JMM
    UE TransformVector 学习笔记
    go语言面试(第一轮)请你说说 TCP 和 UDP 的区别
    Vue3+Vite3+Vant初体验及踩过的一些坑
    MYSQL之DML(数据库操作语言)
    JS三大运行时全面对比:Node.js vs Bun vs Deno
    分析:通过哪种方法来建立股票量化交易数据库?
    pyinstaller打包教程及问题处理
  • 原文地址:https://blog.csdn.net/MRJJ_9/article/details/132629409