一、情景说明
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>