当前位置: 首页 > news >正文

VUE之路由Props、replace、编程式路由导航、重定向

 

目录

1、路由_props的配置

2、路由_replaces属性

3、编程式路由导航

4、路由重定向


1、路由_props的配置

1)第一种写法,将路由收到的所有params参数作为props传给路由组件

只能适用于params参数

// 创建一个路由器,并暴露出去// 第一步:引入createRouter
import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'
// 引入一个个可能呈现组件
import Home from '@/pages/Home.vue'
import News from '@/pages/News.vue'
import About from '@/pages/About.vue'
import Detail from '@/pages/Detail.vue'
// 第二步:创建路由器
const router = createRouter({history:createWebHashHistory(), //路由器的工作模式routes:[  //一个个路由规则{path:'/home',component: Home},{path:'/news',component: News,children:[{name: 'xiangqi',path:'detail/:id/:title/:content?', //用于params参数占位,加?号可传可不传// path:'detail',component:Detail,//第一种写法:将路由收到的所有params参数作为props传给路由组件props: true]},{path:'/about',component: About},]
})export default router

component:Detail,
                //第一种写法:将路由收到的所有params参数作为props传给路由组件
props: true

相当于:<Detail id=??  title = ??  content=??/>

<template><ul class="news-list"><li>编号:{{id}}</li><li>编号:{{title}}</li><li>编号:{{content}}</li></ul>
</template><script lang="ts" setup name="Detail">defineProps(['id','title','content'])
</script>
<template><div class="news"><!--导航区--><ul><li v-for="news in newList" :key="news.id"><RouterLink :to="{name:'xiangqi',params: {  //params不能传对象和数组id:news.id,title:news.title,content:news.content}}">{{news.title}}</RouterLink>         </li></ul><!--展示区--><div class="news-content"><RouterView/></div></div>
</template><script setup lang="ts" name="News">
import { reactive } from 'vue';
import { RouterView,RouterLink } from 'vue-router';
const newList = reactive([{id:'dasfadadadad01',title:'很好的抗癌食物',content:'西蓝花'},{id:'dasfadadadad02',title:'如何一夜暴富',content:'学IT'},{id:'dasfadadadad03',title:'震惊万万没想到',content:'明天周一'},{id:'dasfadadadad04',title:'好消息',content:'快过年了'},])</script>

2)第二种写法,函数写法,可以自己决定将什么作为props给路由组件

适用于query参数类型的传递

 {path:'/news',component: News,children:[{name: 'xiangqi',path:'detail/:id/:title/:content?', //用于params参数占位,加?号可传可不传// path:'detail',component:Detail,//第二种写法:可以自己决定将什么作为props给路由组件props(route){console.log('@@@@',route)return route.params}}]}

其中:console.log打印的route参数结构如下: 

3)第三种写法,对象写法,只能写死,不适用

 {path:'/news',component: News,children:[{name: 'xiangqi',path:'detail/:id/:title/:content?', //用于params参数占位,加?号可传可不传// path:'detail',component:Detail,props:{a: 100,b: 200,c: 300}}]},

2、路由_replaces属性

1)作用:控制路由跳转时操作浏览器历史记录的模式。

2)浏览器的历史记录有两种写入方式:分别为push和replace:

  • push 是追加历史记录(默认值)
  • replace是替换当前记录

3)开启replace模式:

<RouterLink replace .....>News</RouterLink>
    <!--导航区--><div class="navigate"><RouterLink replace  to="/home" active-class="active">首页</RouterLink><RouterLink replace to="/news" active-class="active">新闻</RouterLink><RouterLink replace :to="{path:'/about'}" active-class="active">关于</RouterLink></div>

3、编程式路由导航

 脱离<RouterLink>实现跳转

<script setup lang="ts" name = "Home">
import {onMounted} from 'vue'
import { useRouter } from 'vue-router';const router = useRouter()onMounted(()=>{setTimeout(()=>{// 在此次编写一段代码,让路由实现跳转router.push('/news')},3000)
})</script>
<template><div class="news"><!--导航区--><ul><li v-for="news in newList" :key="news.id"><button @click="showNewsDetail(news)">查看新闻</button><RouterLink :to="{name:'xiangqi',params: {  //params不能传对象和数组id:news.id,title:news.title,content:news.content}}">{{news.title}}</RouterLink>         </li></ul><!--展示区--><div class="news-content"><RouterView/></div></div>
</template><script setup lang="ts" name="News">
import { reactive } from 'vue';
import { RouterView,RouterLink,useRouter} from 'vue-router';
const newList = reactive([{id:'dasfadadadad01',title:'很好的抗癌食物',content:'西蓝花'},{id:'dasfadadadad02',title:'如何一夜暴富',content:'学IT'},{id:'dasfadadadad03',title:'震惊万万没想到',content:'明天周一'},{id:'dasfadadadad04',title:'好消息',content:'快过年了'},])const router = useRouter()interface NewsInter {id:string,title:string,content:string
}function showNewsDetail(news:NewsInter){router.push({name:'xiangqi',params: {  //params不能传对象和数组id:news.id,title:news.title,content:news.content}
})
}
</script>

编程式路由导航应用场景:

1、满足某些条件才跳转

2、鼠标划过就跳转

4、路由重定向

const router = createRouter({history:createWebHashHistory(), //路由器的工作模式routes:[  //一个个路由规则{path:'/home',component: Home},{path:'/about',component: About},{path: '/',redirect: '/home'}]
})export default router
http://www.lryc.cn/news/524972.html

相关文章:

  • windows安装ES
  • 论文速读|Multi-Modal Disordered Representation Learning Network for TBPS.AAAI24
  • 小哆啦解题记:加油站的奇幻冒险
  • 【前端】CSS实战之音乐播放器
  • Games104——渲染中光和材质的数学魔法
  • impala增加字段,hsql查不到数据
  • SpringBoot项目中的异常处理
  • ComfyUI实现老照片修复——AI修复老照片(ComfyUI-ReActor / ReSwapper)尚待完善
  • NLTK命名实体识别(NER)
  • 【游戏设计原理】78 - 持续注意力
  • Android设备:Linux远程lldb调试
  • 多层 RNN原理以及实现
  • [Computer Vision]实验三:图像拼接
  • 【Vim Masterclass 笔记22】S09L40 + L41:同步练习11:Vim 的配置与 vimrc 文件的相关操作(含点评课内容)
  • 5.9 洞察 OpenAI - Translator:日志(Logger)模块的 “时光记录仪”
  • 客户案例:电商平台对帐-账单管理(亚马逊amazon)
  • IP协议特性
  • Kubernetes入门学习
  • 支持向量机SVM的应用案例
  • Chrome 132 版本新特性
  • (5)STM32 USB设备开发-USB键盘
  • Linux 系统服务开机自启动指导手册
  • 分布式多卡训练(DDP)踩坑
  • Codeforces Round 1000 (Div. 2)-C题(树上两个节点不同边数最大值)
  • C++17 新特性解析:Lambda 捕获 this
  • Spring Boot 使用 Micrometer 集成 Prometheus 监控 Java 应用性能
  • Spring Boot 事件驱动:构建灵活可扩展的应用
  • IM系统设计
  • 华为EC6110T-海思Hi3798MV310_安卓9.0_通刷-强刷固件包
  • ASP.NET Blazor托管模型有哪些?