Vue3+Vue Router跳转相同路由监听页面刷新并执行某个操作

1 起源

最近遇到了个这样的需求,大概就是:点击某个按钮,进入某个页面,然后再在这个页面执行某个操作(比如请求某个接口、赋初始值啥的)。

这个需求看似简单,其实也不难。但是, 我遇到了个问题,就是当在那个页面点击这个按钮的时候,因为跳转路由路径是一样的原因,页面是不会刷新的,那我怎么判断我是否我是否点击了那个按钮并且跳到了这个页面呢?

于是,我想到了路由传参,通过路由传参的方式,判断这个参数是否变化了,变化了就代表这个路由再次进入了。

2 解决方案

用query的方式传参,参数附上时间戳,这样每进来一次都是不同的参数

点击按钮如下操作:

javascript 复制代码
const router = useRouter()
​
const goDocumentNotification = () => {
  router.push({
    path: `/documentNotification`,
    query: {
      t: Date.now()
    }
  })
}

在进入的那个页面增加如下代码:

scss 复制代码
// 使用 watch 监听 route 的变化
watch(
  () => route.query.t,
  (newPath, oldPath) => {
  // 路由变化,执行相应操作
    query()
  }
)

ok,经过上面的操作便可以在跳转相同路由下,监听页面刷新并执行某个操作啦。

3 知识扩展-关于Vue Router路由传参的几种常用方式

说到这里,vue-router传参的几种方式也顺便总结一下吧

3.1 params传参(显示参数)

浏览器里路由地址显示为这样:

bash 复制代码
http://127.0.0.1:5190/drs/index.html#/documentNotification/0

声明式:

javascript 复制代码
// 子路由配置
{
    path: '/documentNotification/:id?', // ?代表这个参数为可传可不传
    name: 'documentNotification',
    component: () => import('@/views/documentNotification/index.vue'),
    meta: {
      title: '发放通知',
    }
}
// 父路由组件
<router-link :to="/documentNotification/123">进入documentNotification路由</router-link>

编程式:

dart 复制代码
// 子路由配置
{
    path: '/documentNotification/:id?', // ?代表这个参数为可传可不传
    name: 'documentNotification',
    component: () => import('@/views/documentNotification/index.vue'),
    meta: {
      title: '发放通知',
    }
}
// 父路由编程式传参(一般通过事件触发)
router.push({
    path:'/documentNotification/${yourParam}',
})

关于参数的获取:

csharp 复制代码
route.params.id

3.2 params传参(不显示参数)

由于从Vue Router的2022-8-22 这次更新后,便不能再用这种方式来写,关于不显示参数的传参,可以参考下面这篇博客:

blog.csdn.net/m0_57033755...

3.3 query传参

浏览器里路由地址显示为这样:

bash 复制代码
http://localhost:3000/#/documentNotification?t=1700140985974•

声明式:

javascript 复制代码
//子路由配置
{
    path: '/documentNotification',
    name: 'documentNotification',
    component: () => import('@/views/documentNotification/index.vue'),
    meta: {
      title: '发放通知'
    }
}
//父路由组件
<router-link :to="{name:'documentNotification',query:{t:123}}">进入documentNotification路由</router-link>

编程式:

css 复制代码
//子路由配置
{
    path: '/documentNotification',
    name: 'documentNotification',
    component: () => import('@/views/documentNotification/index.vue'),
    meta: {
      title: '发放通知'
    }
}
router.push({
    path: `/documentNotification`,
    query: {
      t: Date.now()
    }
})

关于参数的获取:

erlang 复制代码
route.query.t

4 结语

ok ,就到这里啦,对此你有何看法或想法呢,欢迎提出讨论呀~

相关推荐
LuciferHuang1 小时前
震惊!三万star开源项目竟有致命Bug?
前端·javascript·debug
GISer_Jing1 小时前
前端实习总结——案例与大纲
前端·javascript
天天进步20151 小时前
前端工程化:Webpack从入门到精通
前端·webpack·node.js
姑苏洛言2 小时前
编写产品需求文档:黄历日历小程序
前端·javascript·后端
知识分享小能手2 小时前
Vue3 学习教程,从入门到精通,使用 VSCode 开发 Vue3 的详细指南(3)
前端·javascript·vue.js·学习·前端框架·vue·vue3
姑苏洛言3 小时前
搭建一款结合传统黄历功能的日历小程序
前端·javascript·后端
你的人类朋友4 小时前
🤔什么时候用BFF架构?
前端·javascript·后端
知识分享小能手4 小时前
Bootstrap 5学习教程,从入门到精通,Bootstrap 5 表单验证语法知识点及案例代码(34)
前端·javascript·学习·typescript·bootstrap·html·css3
一只小灿灿4 小时前
前端计算机视觉:使用 OpenCV.js 在浏览器中实现图像处理
前端·opencv·计算机视觉