文章目录
- 1、onLoad
- 2、onLaunch
- 3、uni.getLaunchOptionsSync()
- 4、getCurrentPages()
- 5、getCurrentPages().pop().options
1、onLoad
javascript
// 页面中常用获取方法
export default {
onLoad(options) {
console.log('onLoad自带参数', options)
},
}
2、onLaunch
javascript
// App.vue
export default {
onLaunch() {
const res = uni.getLaunchOptionsSync();
// 获取启动参数
const { id, type } = res.query;
console.log('APP启动参数:', id, type);
}
}
3、uni.getLaunchOptionsSync()
javascript
// 在任意页面获取项目启动时的参数
const launch = uni.getLaunchOptionsSync();
console.log('启动参数(全局):', launch.query);
4、getCurrentPages()
javascript
// 在页面中其他位置获取
export default {
onShow() {
const params = this.getPageParams();
console.log(params);
},
methods: {
getPageParams() {
const pages = getCurrentPages();
const currPage = pages[pages.length - 1];
return currPage.options || {};
}
}
}
5、getCurrentPages().pop().options
javascript
// 在页面中其他位置获取
export default {
created() {
console.log('created参数', getCurrentPages().pop().options)
},
onLoad(options) {
console.log('onLoad自带参数', options)
console.log('onLoad API获取参数', getCurrentPages().pop().options)
},
mounted() {
console.log('mounted参数', getCurrentPages().pop().options)
},
onShow() {
console.log('onShow参数', getCurrentPages().pop().options)
}
}