Vue3:编程式路由导航

一、情景说明

Vue3的用法略有区别

Vue2的用法:https://blog.csdn.net/Brave_heart4pzj/article/details/136326608

二、案例

简单理解:RouterLink中to怎么写,router.replace或router.push中就怎么配置。

html 复制代码
<!-- 跳转的按钮 -->
<button @click="showNewsDetail(news)">查看新闻</button>

<script setup lang="ts" name="News">
  import {reactive} from 'vue'
  import {RouterView,RouterLink,useRouter} from 'vue-router'

  const newsList = reactive([
    {id:'asfdtrfay01',title:'很好的抗癌食物',content:'西蓝花'},
    {id:'asfdtrfay02',title:'如何一夜暴富',content:'学IT'},
    {id:'asfdtrfay03',title:'震惊,万万没想到',content:'明天是周一'},
    {id:'asfdtrfay04',title:'好消息!好消息!',content:'快过年了'}
  ])

  const router = useRouter()

  interface NewsInter {
    id:string,
    title:string,
    content:string
  }
  //replace方式跳转
  function showNewsDetail(news:NewsInter){
    router.replace({
      name:'xiang',
      query:{
        id:news.id,
        title:news.title,
        content:news.content
      }
    })
  }
  
  //push方式跳转
  function showNewsDetail(news:NewsInter){
    router.push({
      name:'xiang',
      query:{
        id:news.id,
        title:news.title,
        content:news.content
      }
    })
  }
</script>
相关推荐
程序员大阳8 分钟前
副队长Vue3教程(33)--路由懒加载
vue.js·性能优化·vue3·路由·懒加载·速度慢
亲亲小宝宝鸭13 分钟前
Cesium启动:TLE数据是如何变成轨迹线的
前端·cesium
阿里云云原生16 分钟前
对话即构建,在 AgentCore 构建 Agent 就是这么简单
前端·agent
夕水1 小时前
我给我的诗写了个后台,顺手写了 一个组件库ew-solid-ui
前端·javascript·typescript
KevinWang_2 小时前
iOS 开发计划(五) - 做一个离线音乐播放器
javascript
狗头大军之江苏分军2 小时前
前端转行干能干什么?我转行试了下(被迫)
前端·后端
孟陬2 小时前
Foobar 技术周刊#2:约会已死?用户急剧下滑,约会App大概率会弱化约会属性转向轻陪伴。我有个建议:
前端·后端·rust
parade岁月3 小时前
VTable Guild 3.0.0 发布,面向 Vue 3 的 ProTable 式业务表格
前端
xurileidian3 小时前
第 17 篇 · 边玩边学Python · for 遍历 enumerate 与 zip
java·前端·python
掘金安东尼4 小时前
更多审查智能体,更少盲区:GitHub Copilot 代码审查是怎么做的
前端·github copilot