二級(jí)頁面觸發(fā)一級(jí)頁面:
二級(jí)頁面代碼:
uni.navigateBack({
delta: 1,
success() {
const pages = getCurrentPages();
const targetPage = pages[0];
targetPage.$vm.addJournalState=true
}
})
有人部分博客獲取上一頁信息是通過這樣獲取的:var targetPage = pages[pages.length - 2],我這樣獲取顯示undefined,大家可以把a(bǔ)ges打印一下,看看內(nèi)容,然后自行選擇
注解:
getCurrentPages()
方法來獲取當(dāng)前頁面棧
$vm
是指Vue組件實(shí)例
一級(jí)頁面代碼:
onShow() {
setTimeout(() => {
if (this.addJournalState) {
// 重新獲取數(shù)據(jù)
this.upJournal()
this.addJournalState = false
}
}, 100)
}
每次頁面切換就會(huì)判斷一下,如何是指定二級(jí)頁面返回過來,會(huì)將狀態(tài)設(shè)置為ture,就會(huì)執(zhí)行代碼重新獲取數(shù)據(jù),其他則不會(huì)
注解:
使用setTimeout的原因是因?yàn)榇_認(rèn)一級(jí)頁面addJournalState狀態(tài)已經(jīng)被修改
頁面跳轉(zhuǎn)傳遞傳參
跳轉(zhuǎn)頁:
uni.navigateTo({
url: '/pages/material/purchaseDetails?data=' + encodeURIComponent(JSON
.stringify(argumentAll))
})
接受頁:
onLoad(options) {
// 獲取頁面?zhèn)鬟f的對象參數(shù)
const data = JSON.parse(decodeURIComponent(options.data));
this.dataObj = data
}
注解:
encodeURIComponent
是 JavaScript 中的一個(gè)函數(shù),用于對 URI(統(tǒng)一資源標(biāo)識(shí)符)中的特殊字符進(jìn)行編碼。它將指定的字符串作為參數(shù),并返回一個(gè)新的字符串,其中包含了經(jīng)過編碼的特殊字符
decodeURIComponent
是 JavaScript 中的一個(gè)函數(shù),用于解碼經(jīng)過 encodeURIComponent
編碼的 URI(統(tǒng)一資源標(biāo)識(shí)符)字符串。它將編碼后的字符串作為參數(shù),并返回一個(gè)新的字符串?
轉(zhuǎn)發(fā)功能?
效果圖:
轉(zhuǎn)發(fā)功能:?
<template>
<view class="container">
<button class="share-btn" open-type="share">轉(zhuǎn)發(fā)</button>
</view>
</template>
<script>
export default {
data() {
return {};
},
onShareAppMessage() {
return {
title: '這是分享的標(biāo)題',
path: '/pages/index/index',
imageUrl: '/static/share.jpg'
}
},
};
</script>
<style scoped>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.share-btn {
width: 200rpx;
height: 80rpx;
background-color: #007aff;
color: #ffffff;
border-radius: 40rpx;
font-size: 32rpx;
line-height: 80rpx;
text-align: center;
}
</style>
注解:
open-type="share"
表示將該按鈕設(shè)置為小程序默認(rèn)的分享按鈕。
onShareAppMessage()
是小程序提供的一個(gè)生命周期函數(shù),當(dāng)用戶點(diǎn)擊分享按鈕時(shí)會(huì)自動(dòng)調(diào)用該函數(shù),并且根據(jù)該函數(shù)返回的分享內(nèi)容進(jìn)行分享
title
屬性,表示小程序分享的標(biāo)題,這里設(shè)置為“這是分享的標(biāo)題”。
path
屬性,表示小程序分享的路徑,這里設(shè)置為小程序首頁的路徑/pages/index/index
。文章來源:http://www.zghlxwxcb.cn/news/detail-521222.html
imageUrl
屬性,表示小程序分享時(shí)的封面圖,這里設(shè)置為靜態(tài)資源目錄下的一張圖片。文章來源地址http://www.zghlxwxcb.cn/news/detail-521222.html
到了這里,關(guān)于uniapp開發(fā)小程序,當(dāng)二級(jí)頁面返回一級(jí)頁面時(shí),觸發(fā)一級(jí)頁面的事件或者更新一級(jí)頁面的某項(xiàng)數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!