javascript - vue.js 异步回调函数
问题描述
我有一个模态组件,它只是 window.confirm 的一个花哨版本,我通过注入插件使它在我的 nuxt 应用程序中的任何地方都可以调用。
组件/confirmModal.vue
<template>
<transition name="fade">
<div
v-if="isShow"
@click="handleClickOverlay"
class="confirm-modal-overlay"
id="swConfirm"
>
<transition name="zoom">
<div v-if="isShow" ref="swConfirmDialog" class="confirm-modal-container">
<span class="confirm-modal-text-grid">
<h4 v-if="dialog.title" class="confirm-modal-title">{{ dialog.title }}</h4>
<p v-if="dialog.message" class="confirm-modal-text">{{ dialog.message }}</p>
</span>
<div
class="confirm-modal-btn-grid"
:class="{ isMono: !dialog.button.no || !dialog.button.yes }"
>
<button
v-if="dialog.button.no"
@click.stop="e => handleClickButton(e, false)"
class="confirm-modal-btn left"
>
{{ dialog.button.no }}
</button>
<button
v-if="dialog.button.yes"
@click.stop="e => handleClickButton(e, true)"
class="confirm-modal-btn"
>
{{ dialog.button.yes }}
</button>
</div>
</div>
</transition>
</div>
</transition>
</template>
<script>
import Vue from 'vue'
import { events } from '../plugins/confirm-modal'
Vue.directive('focus', {
inserted: function(el) {
el.focus()
}
})
const Component = {
name: 'swConfirmDialog',
data() {
return {
isShow: false,
dialog: {
title: '',
message: '',
button: {}
},
params: {}
}
},
methods: {
resetState() {
this.dialog = {
title: '',
message: '',
button: {},
callback: () => {}
}
},
handleClickButton({ target }, confirm) {
if (target.id == 'swConfirm') return
this.isShow = false
if (this.params.callback) {
this.params.callback(confirm)
}
},
handleClickOverlay({ target }) {
if (target.id == 'swConfirm') {
this.isShow = false
// callback
if (this.params.callback) {
this.params.callback(false)
}
}
},
open(params) {
this.resetState()
this.params = params
this.isShow = true
Object.entries(params).forEach(param => {
if (typeof param[1] == typeof this.dialog[param[0]]) {
this.dialog[param[0]] = param[1]
}
})
},
registerEvents() {
events.$on('open', this.open)
events.$on('close', () => {
this.handleClickOverlay({ target: { id: 'swConfirm' } })
})
}
},
mounted() {
this.registerEvents()
}
}
export default Component
</script>
插件/确认-modal.js
import SwConfirmModal from "~/components/SwConfirmModal.vue"
import Vue from "vue"
export const events = new Vue({
name: 'sw-confirm-modal'
})
export default (ctx, inject) => {
Vue.component('sw-confirm-modal', SwConfirmModal)
const confirm = params => {
events.$emit('open', params)
}
confirm.close = () => {
events.$emit('close')
}
ctx.$confirm = confirm
inject("confirm", confirm)
}
我在我的组件中调用它,就像下面的代码一样,但是因为我想在我的回调中执行异步函数,所以我一直收到错误"Syntax Error: Can not use the keyword 'await' outside an async function"
methods: {
logout(){
this.$confirm(
{
message: `confirm logout?`,
button: {
no: 'Cancel',
yes: 'OK'
},
callback: confirm => {
if (confirm) {
let res = await this.$auth.logout()
}
}
}
)
}
}
解决方案
只需将回调更改为异步
callback: async (confirm) => {
if (confirm) {
let res = await this.$auth.logout()
}
}
推荐阅读
- c# - ASP.NET Core 3.1 如何让控制器使用 System.Text.Json 进行序列化和反序列化
- python - Django elasticsearch无法导入模块
- node.js - 如何在 Win10 上的 Intellij 中设置 npm 依赖项更新
- javascript - 更改委托实例的 Tippy 内容
- c# - 我无法访问 c# Xamarin 中的 json 文件
- go - 将弹性搜索与 Logrus 集成(golang 日志记录)
- python - 使用python在特定路径中创建文件
- javascript - 如何在 javascript 函数中访问 Mustache 文件 [更新]?
- css - 如何使用react js的app.css修复右侧导航栏上重叠的导航栏下拉菜单
- c++ - lower_bound() STL 中的自定义比较器