javascript - Vue继续成功回调
问题描述
我有一个具有 Vuex 依赖项的 Vue 应用程序。程序逻辑是Facebook分享成功后发送消息。
这是在单击按钮以发送消息时触发的方法:
onSubmit() {
if(this.shouldShowShareModal){
this.$store.dispatch('openModal', 'SHARE_MODAL');
return;
}
this.$store.dispatch('sendMessage', {
conversation_id: this.conversationId,
message : this.messageText,
message_type_id: 1
}).then(() => {
this.messageText = '';
this.scrollToBottom();
});
},
openModal
只需将给定模态的值设置为 true,从而v-if
显示具有在share
方法上触发 Facebook 分享的按钮的模态:
share() {
var self = this;
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/'
}, function(){
self.$store.dispatch('sharedThroughFacebook');
self.$store.dispatch('closeModal', 'SHARE_MODAL');
});
}
现在我遇到的问题是 Facebook 回调成功后如何继续发送消息?目前,sharedThroughFacebook
我只是将商店共享标志设置为 true,但我不确定仅在成功回调后发送消息的最佳方法?如果我将数据推送到模态,那似乎是一个肮脏的解决方案,并且模态不应该知道消息状态。另一方面,将对话 ID、消息、类型和文本放在 Vuex 存储中似乎是一种开销,因为该组件是唯一使用数据的组件。
解决方案
你可以像这样组成你的动作
actions: {
async share ({ dispatch, commit }) {
await dispatch('sharedThroughFacebook') // wait for `sharedThroughFacebook` to finish
await dispatch('closeModal') // close the modal before sending the message
commit('sendMesseage')
},
async sharedThroughFacebook (context, payload) {
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/'
})
}
}
推荐阅读
- asynchronous - 如何从带有回调的 C# 方法创建 F# 异步?
- ios - 在“文件”应用程序中点击文件时iOS打开应用程序
- java - Digital Ocean java服务器不断崩溃:java.net.SocketException:连接重置
- python - 记住一张图片需要多少参数
- node.js - 使用 node-fetch 跳过等待时间
- python - 如何检查一个数组是否在Python中的另一个数组中
- regex - 使用 grep 在 XmlPath 表达式中查找部分字符串匹配
- jenkins - 为什么詹金斯突然无法删除工作区
- java - Android Firestore,如何在一个文档中设置多个时间戳
- python - 如何将文本文件(或字符串到 QML 中的 textArea)从 python 发送到 QML 应用程序?