路由跳转前发生的事件
router.beforeEach((to, from, next) => {
console.log(to)
next()
})
路由跳转后发生的事件
router.afterEach((to, from) => {
console.log(to)
sessionStorage.setItem('sTop',0)
setTimeout(function () {
//滚动条回到顶部
window.scrollTo(0,sTop);
},500)
})
详情页跳转
路由配置
{
path:'/shop/detail/:id',
name:'shopDetail',
component: shopDetail,
meta:{
title:'商品详情',
}
},
跳转
this.$router.push('/shop/detail/'+id)
路由的两种模式
const router = new Router({
//mode : 'history',//不带#号
//mode : 'hash',//默认模式hash
routes: [
{
path: '/',
name: 'login',
component: login,
meta: {
title: '登录'
}
},
]
})
返回上一页
this.$router.go(-1)

本文介绍Vue.js中路由的使用方法,包括路由跳转前后事件的处理、页面滚动位置的控制技巧以及不同路由模式的选择。同时展示了如何通过代码实现具体的页面跳转和返回操作。
1468

被折叠的 条评论
为什么被折叠?



