Vue - route路由跳转

前言

vue中的route实现了从一个页面跳转到另一个页面的功能


基本路由跳转

  • router.js
import { createRouter, createWebHashHistory } from 'vue-router'
import Home from '../views/home.vue'
import Detail from '../views/detail'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/detail',
    name: 'Detail',
    component: Detail
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router

router-link 跳转路由

  • 实现首页跳转详情页

在这里插入图片描述


  • route-link相当于a标签跳转,其指定route-view显示的内容
  • App.vue
<template>
  <div>
  	<!-- route-view 显示一个route所对应的页面 -->
    <router-view/>
  </div>
</template>

<script>

export default {
  name: 'App'
}
</script>
  • home.vue
<template>
  <div>首页</div>
  <router-link to="/detail">Detail</router-link>
</template>

<script>
export default {
  name: 'home'
}
</script>
  • detail.vue
<template>
  <div>详情</div>
</template>

<script>
export default {
  name: 'detail'
}
</script>

router.push(path) 跳转路由

  • 通过路由的path属性实现首页跳转详情页

在这里插入图片描述

  • App.vue
<template>
  <div>
    <router-view @route-change="onRouteChange"/>
  </div>
</template>

<script>
import router from '@/router'

export default {
  name: 'App',
  methods: {
    /**
     * 自定义事件监听
     * @param event
     */
    onRouteChange (event) {
      console.log(event)
      router.push('/detail')
    }
  }
}
</script>
  • home.vue
<template>
  <div>首页</div>
  <img @click="toDetail" src="@/assets/logo.png" style="width: 100%;"/>
</template>

<script>
export default {
  name: 'home',
  // 定义抛出的事件名称
  emits: ['route-change'],
  methods: {
    toDetail (event) {
      // 自定义事件抛出
      this.$emit('route-change', event)
    }
  }
}
</script>

router.push(name) 跳转路由

  • 通过路由的name属性实现首页跳转详情页

在这里插入图片描述

  • App.vue
<template>
  <div>
    <router-view @route-change="onRouteChange"/>
  </div>
</template>

<script>
import router from '@/router'

export default {
  name: 'App',
  methods: {
    /**
     * 自定义事件监听
     * @param event
     */
    onRouteChange (event) {
      router.push({
        name: event.name
      })
    }
  }
}
</script>
  • home.vue
<template>
  <div>首页</div>
  <img @click="toDetail" src="@/assets/logo.png" style="width: 100%;"/>
</template>

<script>
export default {
  name: 'home',
  // 定义抛出的事件名称
  emits: ['route-change'],
  methods: {
    toDetail (event) {
      // 自定义事件抛出
      this.$emit('route-change', { name: 'Detail' })
    }
  }
}
</script>

嵌套路由跳转

  • 实现详情页跳转详情子页面

在这里插入图片描述


  • route.js
{
  path: '/detail',
  name: 'Detail',
  component: Detail,
  children: [
    {
      path: '',
      name: 'DetailHome',
      component: DetailHome
    },
    {
      path: 'sub',
      name: 'SubDetail',
      component: SubDetail
    }
  ]
}
  • detail.vue
<template>
  <router-view @to-sub-detail="toSubDetail"/>
</template>

<script>
import router from '@/router'
export default {
  name: 'detail',
  methods: {
    /**
     * 自定义事件监听
     */
    toSubDetail () {
      router.push('/detail/sub')
    }
  }
}
</script>
  • detail-home.vue
<template>
  <div>详情页</div>
  <button @click="toSubDetail">跳转子页面</button>
  <router-view/>
</template>

<script>
export default {
  name: 'DetailHome',
  methods: {
    toSubDetail () {
      // 自定义事件抛出
      this.$emit('to-sub-detail')
    }
  }
}
</script>
  • sub-detail.vue
<template>
    <div>详情子页面</div>
</template>

<script>
export default {
  name: 'SubDetail'
}
</script>

- End -
- 个人学习笔记 -
- 仅供参考 -

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maggieq8324

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值