6***@qq.com
6***@qq.com
  • 发布:2023-03-08 17:01
  • 更新:2023-03-10 20:10
  • 阅读:530

通过 vue-cli 创建的项目,vue3.0 <script setup>,怎么调用前一页的方法?

分类:uni-app

A页面:

<script setup lang="ts">  
//...  
const todo = () => {  
    //...  
}  
//...  
</script>

B页面返回A页面:

let pages = getCurrentPages(); // 当前页面  
let beforePage = pages[pages.length - 2]; // 需要执行方法的页面  
uni.navigateBack({  
    success () {  
          if (beforePage.$vm.todo)  
               beforePage.$vm.todo();//执行页面中的自定义的init方法  
    }  
})

调用不了上一页的todo方法,但是可以调用onLoad生命周期方法,但不想用这个。
请问我应该怎么做?

2023-03-08 17:01 负责人:无 分享
已邀请:
YUANRJ

YUANRJ

1.A页面 将需要调用的方法暴露在当前实例上

<script setup lang="ts">  
    const todo = () => {  
        // ...  
    }  
    defineExpose({  
        todo  
    })  
</script>

2.B页面 通过以下方法进行调用

<script setup lang="ts">    
    const pages = getCurrentPages()  
    const beforePage = pages[pages.length - 2]  
    beforePage.$vm.$.exposed.todo()  
</script>  

另外也可以使用uni-app内置的页面通讯方法EventChannel

  • 6***@qq.com (作者)

    明白了,谢谢

    2023-04-06 15:47

6***@qq.com

6***@qq.com (作者)

是不是只能options形式才行?

要回复问题请先登录注册