Vue3:编程式路由导航

一、情景说明

Vue3的用法略有区别

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

二、案例

简单理解:RouterLinkto怎么写,router.replacerouter.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>
相关推荐
共享家95274 分钟前
QT-常用控件(多元素控件)
开发语言·前端·qt
幸运小圣5 分钟前
Iterator迭代器 【ES6】
开发语言·javascript·es6
葱头的故事6 分钟前
将传给后端的数据转换为以formData的类型传递
开发语言·前端·javascript
中微子17 分钟前
🚀 2025前端面试必考:手把手教你搞定自定义右键菜单,告别复制失败的尴尬
javascript·面试
_233318 分钟前
vue3二次封装element-plus表格,slot透传,动态slot。
前端·vue.js
jump68021 分钟前
js中数组详解
前端·面试
崽崽长肉肉24 分钟前
iOS 基于Vision.framework从图片中提取文字
前端
温宇飞30 分钟前
Web Abort API - AbortSignal 与 AbortController
前端
Tomoon31 分钟前
前端开发者的全栈逆袭
前端