写得不好,只是实现了功能,可以自己优化一下,我测了放onMounted和onReady下都可以
// useShare.js  
import { onShareAppMessage,onShareTimeline } from '@dcloudio/uni-app'  
export const useShare = (params = {}) => {  
    let defaultOptions = {  
        title: '全局分享标题',  
        path: '/pages/index/index',  
        ...params  
    }  
    // 分享朋友默认配置  
    let shareAppOptions = {}  
    // 分享朋友圈默认配置  
    let shareTimeOptions = {}  
    // onShareAppMessage  
    const shareApp = (options = {}) => {  
        onShareAppMessage((res) => {  
            return {  
                ...defaultOptions,  
                ...options,  
                ...shareAppOptions  
            }  
        })  
    }  
    // 添加onShareAppMessage参数  
    const setShareApp = (options = {}) => {  
        shareAppOptions = options  
    }  
    // onShareTimeline  
    const shareTime = (options = {}) => {  
        onShareTimeline(() => {  
            return {  
                ...defaultOptions,  
                ...options,  
                ...shareTimeOptions  
            }  
        })  
    }  
    // 添加onShareTimeline参数  
    const setShareTime = (options = {}) => {  
        shareTimeOptions = options  
    }  
    return {  
        onShareAppMessage: shareApp,  
        onShareTimeline: shareTime,  
        setShareApp,  
        setShareTime,  
    }  
}
// index.vue  
<script setup>  
    import { useShare } from '@/hooks/useShare.js'  
    import { onMounted } from 'vue'  
    import { onLoad,onReady } from '@dcloudio/uni-app'  
    const {  
        onShareAppMessage,  
        onShareTimeline,  
        setShareApp,  
        setShareTime  
    } = useShare()  
    onLoad(() => {  
        setOptions()  
    })  
    onShareAppMessage()  
    onShareTimeline({  
        title: '自定义标题',  
        path: '/pages/index/dark'  
    })  
    // onMounted(() => {  
    //  onShareAppMessage({  
    //      title: '自定义标题onMonted',  
    //      path: '/pages/index/mine'  
    //  })  
    // })  
    // onReady(() => {  
    //  onShareTimeline({  
    //      title: '自定义标题onReady',  
    //      path: '/pages/index/dark'  
    //  })  
    // })  
    const mockApi = () => {  
        return new Promise((resolve) => {  
            setTimeout(() => {  
                resolve({title: '接口返回标题',path: '/pages/index/api'})  
            },200)  
        })  
    }  
    const setOptions = async () => {  
        const res = await mockApi()  
        setShareApp(res)  
    }  
</script>
                                        
                                     
零充
好用。感谢!
2022-06-29 15:26
十二112
回复 零充: 不客气
2022-06-29 15:55
6***@qq.com
大佬,在接口数据加载完成onMounted或者nextTick中调用onShareAppMessage( )并传入参数title path等,然后通过<button open-type="share"> 和 胶囊转发分享,为什么不生效。控制台警告:onShareAppMessage is called when there is no active component instance to be associated with. Lifecycle injection APIs can only be used during execution of setup() 翻译:当没有要关联的活动组件实例时,将调用onShareAppMessage。生命周期注入API只能在执行setup()期间使用;想拿到后台给的数据后,并传入onShareAppMessage,我该如何解决呢?
2022-07-05 13:37
十二112
回复 6***@qq.com: 看下面
2022-07-05 14:21
6***@qq.com
回复 十二112: 可以了 ,thank you
2022-07-05 16:27
十二112
回复 6***@qq.com: 不客气
2022-07-05 16:35