wordpress手机管理,沈阳免费seo关键词优化排名,wordpress 调用侧边栏,制作一个静态网页【1】Vue2编程式路由导航
① router.push
除了使用 router-link 创建 a 标签来定义导航链接#xff0c;我们还可以借助 router 的实例方法#xff0c;通过编写代码来实现。
router.push(location, onComplete?, onAbort?)注意#xff1a;在 Vue 实例内部#…【1】Vue2编程式路由导航
① router.push
除了使用 router-link 创建 a 标签来定义导航链接我们还可以借助 router 的实例方法通过编写代码来实现。
router.push(location, onComplete?, onAbort?)注意在 Vue 实例内部你可以通过 $router 访问路由实例。因此你可以调用 this.$router.push。
想要导航到不同的 URL则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录所以当用户点击浏览器后退按钮时则回到之前的 URL。
当你点击 router-link 时这个方法会在内部调用所以说点击 router-link :to... 等同于调用 router.push(...)。
声明式编程式router-link :to...router.push(...)
该方法的参数可以是一个字符串路径或者一个描述地址的对象。例如
// 字符串
router.push(home)// 对象
router.push({ path: home })// 命名的路由
router.push({ name: user, params: { userId: 123 }})// 带查询参数变成 /register?planprivate
router.push({ path: register, query: { plan: private }})注意如果提供了 pathparams 会被忽略上述例子中的 query 并不属于这种情况。取而代之的是下面例子的做法你需要提供路由的 name 或手写完整的带有参数的 path
const userId 123
// params与name组合使用
router.push({ name: user, params: { userId }}) // - /user/123
//拼接path
router.push({ path: /user/${userId} }) // - /user/123// 这里的 params 不生效 params不可以与path组合使用
router.push({ path: /user, params: { userId }}) // - /user同样的规则也适用于 router-link 组件的 to 属性。
在 2.2.0可选的在 router.push 或 router.replace 中提供 onComplete 和 onAbort 回调作为第二个和第三个参数。这些回调将会在导航成功完成 (在所有的异步钩子被解析之后) 或终止 (导航到相同的路由、或在当前导航完成之前导航到另一个不同的路由) 的时候进行相应的调用。在 3.1.0可以省略第二个和第三个参数此时如果支持 Promiserouter.push 或 router.replace 将返回一个 Promise。
注意 如果目的地和当前路由相同只有参数发生了改变 (比如从一个用户资料到另一个 /users/1 - /users/2)你需要使用 beforeRouteUpdate 来响应这个变化 (比如抓取用户信息)。 ② router.replace
router.replace(location, onComplete?, onAbort?)跟 router.push 很像唯一的不同就是它不会向 history 添加新记录而是跟它的方法名一样 —— 替换掉当前的 history 记录。
声明式编程式router-link :to... replacerouter.replace(...)
使用实例如下
li v-form in messages :keym.idrouter-link :to/home/message/detail/${m.id}{{m.title}}/router-linkbutton clickpushShow(m.id)push查看/buttonbutton clickreplaceShow(m.id)replace查看/button
/li
methods: {pushShow (id) {this.$router.push(/home/message/detail/${id})},replaceShow(id) {this.$router.replace(/home/message/detail/${id})}}总结如下
1) this.$router.push(path): 相当于点击路由链接(可以返回到当前路由界面)
2) this.$router.replace(path): 用新路由替换当前路由(不可以返回到当前路由界面)
3) this.$router.back(): 请求(返回)上一个记录路由
4) this.$router.go(-1): 请求(返回)上一个记录路由
5) this.$router.go(1): 请求下一个记录路由③ 替换路由参数并刷新路由
this.$router.replace({name: concertDetail,query: {concertId: id}
})
/** 兼容性好*/
window.location.reload()【2】Vue3编程式路由导航
路由组件的两个重要的属性$route和$router变成了两个hooks 。route表示当前路由router表示路由器实例。
import {useRoute,useRouter} from vue-routerconst route useRoute()
const router useRouter()
//当前路由信息
console.log(route.query)
console.log(route.parmas)
//路由器实例
console.log(router.push)
console.log(router.replace)① router.replace
templatediv classnews!-- 导航区 --ulli v-fornews in newsList :keynews.idbutton clickshowNewsDetail(news)查看新闻/buttonRouterLink :to{name:xiang,query:{id:news.id,title:news.title,content:news.content}}{{news.title}}/RouterLink/li/ul!-- 展示区 --div classnews-contentRouterView/RouterView/div/div
/templatescript setup langts nameNewsimport {reactive} from vueimport {RouterView,RouterLink,useRouter} from vue-routerconst 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}function showNewsDetail(news:NewsInter){router.replace({name:xiang,query:{id:news.id,title:news.title,content:news.content}})}/script