河南推广网站的公司,贵阳能做网站的公司有哪些,新手开店适合开什么店,天津公司网站如何制作目录
1、路由_props的配置
2、路由_replaces属性
3、编程式路由导航
4、路由重定向 1、路由_props的配置
1#xff09;第一种写法#xff0c;将路由收到的所有params参数作为props传给路由组件
只能适用于params参数
// 创建一个路由器#xff0c;并暴露出去// 第一步…
目录
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??/ templateul classnews-listli编号:{{id}}/lili编号:{{title}}/lili编号:{{content}}/li/ul
/templatescript langts setup nameDetaildefineProps([id,title,content])
/script
templatediv classnews!--导航区--ulli v-fornews in newList :keynews.idRouterLink :to{name:xiangqi,params: { //params不能传对象和数组id:news.id,title:news.title,content:news.content}}{{news.title}}/RouterLink /li/ul!--展示区--div classnews-contentRouterView//div/div
/templatescript setup langts nameNews
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 classnavigateRouterLink replace to/home active-classactive首页/RouterLinkRouterLink replace to/news active-classactive新闻/RouterLinkRouterLink replace :to{path:/about} active-classactive关于/RouterLink/div3、编程式路由导航 脱离RouterLink实现跳转
script setup langts name Home
import {onMounted} from vue
import { useRouter } from vue-router;const router useRouter()onMounted((){setTimeout((){// 在此次编写一段代码让路由实现跳转router.push(/news)},3000)
})/script
templatediv classnews!--导航区--ulli v-fornews in newList :keynews.idbutton clickshowNewsDetail(news)查看新闻/buttonRouterLink :to{name:xiangqi,params: { //params不能传对象和数组id:news.id,title:news.title,content:news.content}}{{news.title}}/RouterLink /li/ul!--展示区--div classnews-contentRouterView//div/div
/templatescript setup langts nameNews
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