在uniapp、mpvue、vue(h5)中如何获取当前页面和地址栏?

4,774 阅读1分钟

获取当前页面

1、在vue(h5)项目中:

this.$route.path   //路由路径
this.$route.params  //路由路径参数
this.$route.query  //路由查询参数

2、在mpvue、uniapp项目中:

const pages = getCurrentPages() //获取加载的页面
const currentPage = pages[pages.length-1] // 获取当前页面栈的实例
// const currentPage =getCurrentPages().pop(); // 获取当前页面栈的实例

// const url = currentPage.route //当前页面url
// const options = currentPage.options //如果要获取url中所带的参数可以查看options
// console.log(options)

//  如果需要重载当前页,在获取到当前页面栈的实例后,可以执行当前页的onLoad(或者onShow、onReady)
// if (currentPage  == undefined || currentPage  == null) return; 
// currentPage.onShow(); 

获取页面url和参数

1、在vue项目中:

(1)、完整url:

window.location.href

(2)、路由路径:

this.$route.path

(3)、路由路径参数:

this.$route.params

例如:

/user/:id → /user/2044011030this.$route.params.id

(4)、路由查询参数:

this.$route.query

例如:

/user/search?name=sf → this.$route.query.name

2、在uniapp、mpvue项目页面中,要获取路由路径参数,可以有以下方法:

(1)、统一到onLoad的参数中获取,其实就是原生的小程序获取方式:

onLoad(options) {    
  let {link,pms,title}=options
}

(2)、先获取到当前页面currentPage(上面已提到),然后currentPage.options 。

3、mpvue项目中

(1)、如何获取小程序在 page onLoad 时候传递的 options

在所有 页面 的组件内可以通过this.root.root.mp.query 进行获取

(2)、如何获取小程序在 app onLaunch/onShow 时候传递的 options

在所有的组件内可以通过this.root.root.mp.appOptions 进行获取