唐鑫
唐鑫
  • 发布:2022-01-11 14:33
  • 更新:2022-04-02 11:29
  • 阅读:2422

uniapp使用vue3和ts跳转页面传值

分类:uni-app

今天在使用vue3模式用ts开发微信小程序时遇到需要向商品详情页面传值
但是列表页面列表项已经包含了商品详情需要的数据,就打算全部传过去
但是在里面我并没有得到this.getOpenerEventChannel();这个对象,通过getCurrentInstance()也并没得到
打算通过uniapp的窗体通信来传递数据
于是这里对此作了一些封装

/**  
 * @description 页面发送数据  
 * @param {string} eventName 事件名称  
 * @param {T} data 需要发送的数据  
 */  
export function pageSendData<T>( eventName : string , data : T ) {  
    // console.log( '等待回应:' , eventName + '_response' );  
    uni.$once( eventName + '_response' , () => {  
        // console.log( '收到回应:' , eventName + '_response' );  
        // console.log( '发送数据到事件:' , eventName );  
        uni.$emit( eventName , data );  
    } );  
}  

declare type pageResultDataCallBack<T> = ( data : T ) => void;  

/**  
 * @description 页面接收数据  
 * @param {string} eventName 事件名称  
 * @param {pageResultDataCallBack} callBack 接收数据回调  
 */  
export function pageResultData<T>( eventName : string , callBack : pageResultDataCallBack<T> ) {  
    // console.log( '准备接收数据:' , eventName );  
    uni.$once( eventName , callBack );  
    // console.log( '发送回应:' , eventName + '_response' );  
    uni.$emit( eventName + '_response' );  
}
1 关注 分享
Gsnow0123

要回复文章请先登录注册

s***@qq.com

s***@qq.com

可以参考一下这个:https://ask.dcloud.net.cn/article/39740
2022-04-02 11:29