uniapp微信小程序获取当前路由地址

最近开发中有跳过或者返回的需求
所以需要获取到当前页面的路径
建议将代码放到公共js里面,所有也能均可调用

 getCurrentRouter() {
      let routes = getCurrentPages() // 获取当前打开过的页面路由数组
      let curRoute = routes[routes.length - 1].route // 获取当前页面路由,也就是最后一个打开的页面路由
      const pageId = routes[routes.length - 1].options.id
      const pageUrl = '/' + curRoute + '?id=' + pageId
      return pageUrl
  },

存入缓存中
如销毁:在离开页面时remove即可

 uni.setStorageSync('pre_redirect_url', this.getCurrentRouter())
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐