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>
相关推荐
IT_陈寒3 小时前
Vue开发实战:从入门到精通
前端·人工智能·后端
znnnk3 小时前
2026 前端工程化新纪元:Rust 重塑工具链,AI Agent 接管流水线
前端·人工智能·rust
王大师王文峰3 小时前
HTML 入门到进阶:语义化标签、表单与多媒体一篇打通【语义化布局、表单校验、video/audio、Canvas、无障碍与 SEO】
前端·css·node.js·html·css3·jquery·html5
风骏时光牛马4 小时前
AI_Coding:智能代码创作助手
前端
剪刀石头布啊12 小时前
vue-react为何要虚拟dom
前端
剪刀石头布啊12 小时前
ahooks常用的一些库,我们使用一下
前端
剪刀石头布啊12 小时前
从git更新本地提交记录到更新已经推送到远端记录
前端
云浪13 小时前
Go 并发实战:从零实现一个高并发安全的任务调度器
javascript·后端·go
Flynt14 小时前
axios 被投毒半年后,我把 npm 的"安全防线"实测了一遍:最信任的 SHA512 是安慰剂
前端·npm·node.js
excel15 小时前
Base62 和 Base64 有什么区别?
前端