微信小程序当前页调用上一页的方法

譬如 filling.vue 有声明 updatePhoto() 函数,就是把拍摄照片上传到云端,然后让View标签加载拍摄的照片。

updatePhoto: function(type, path) {
    let that = this;
    that.uploadCos(that.url.uploadCosPrivateFile, path, 'driverAuth', function(resp) {
        let data = JSON.parse(resp.data);
        that.cosImg.push(data.path);
        if (type == 'idcardHolding') {
            that.cardBackground[2] = path;
            that.currentImg['idcardHolding'] = data.path;
            that.idcard.idcardHolding = data.path;
        } 
    });
    this.$forceUpdate();
},

然后在 vue2 里的写法:

let pages = getCurrentPages(); //你访问的小程序页面历史记录
let prevPage = pages[pages.length - 2]; //上一个小程序页面
//调用上一个页面的updatePhoto函数,回传拍好的照片
prevPage.$vm.updatePhoto(that.type, that.photoPath);
//返回上一个页面
uni.navigateBack({
    delta: 1
});

改在 vue3 里的写法:

// filling.vue 改写如下:
<script setup>
...
const updatePhoto = (photoType, photoPath) => {
  ...
};
...
defineExpose({updatePhoto})
</script>

// 当前页调用如下:
<script setup>
...
const pages = getCurrentPages();
const prevPage = pages[pages.length - 2];
...
prevPage.updatePhoto(type.value, photoPath.value)
...
</script>
posted @ 2026-01-07 15:01  hwq1992  阅读(0)  评论(0)    收藏  举报