要在请求成功后弹出一个“充值成功”的弹窗,你可以使用浏览器的原生alert函数或者更现代的UI框架中的弹窗组件(如Element Plus或Vuetify提供的对话框组件)。
使用 alert
以下是直接使用浏览器提供的alert函数方式:
javascript
async handleSubmit() {
try {
const response = await request({
method: 'POST',
url: `/products/self/pay`,
baseURL: 'http://localhost:8080',
data: {
amount: this.amount,
userName: this.$store.state.user.name,
paymentMethod: this.paymentMethod,
},
});
// 如果请求成功
alert('充值成功');
this.message = `成功充值 ${this.amount} 元,使用的支付方式是 ${this.paymentMethod}`;
} catch (error) {
console.error('Checkout failed:', error);
}
}
使用 Vue UI 框架(如Element Plus)中的对话框
如果你已经使用像Element Plus这样的组件库,你可以用更为美观的对话框组件。
首先,确保你的项目中安装并配置了Element Plus:
bash
npm install element-plus --save
然后在组件中引入和使用。例如:
javascript
<template>
<div class="recharge">
<!-- 你的其他HTML -->
<el-dialog
title="提示"
:visible.sync="dialogVisible"
width="30%"
@close="handleClose">
<span>充值成功!</span>
<span slot="footer" class="dialog-footer">
<el-button @click="handleClose">确认</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import { ElDialog, ElButton } from 'element-plus';
import 'element-plus/lib/theme-chalk/index.css';
export default {
components: {
ElDialog,
ElButton
},
data() {
return {
amount: 0,
paymentMethod: 'alipay',
message: '',
dialogVisible: false
};
},
methods: {
async handleSubmit() {
try {
const response = await request({
method: 'POST',
url: `/products/self/pay`,
baseURL: 'http://localhost:8080',
data: {
amount: this.amount,
userName: this.$store.state.user.name,
paymentMethod: this.paymentMethod,
},
});
// 显示对话框
this.dialogVisible = true;
this.message = `成功充值 ${this.amount} 元,使用的支付方式是 ${this.paymentMethod}`;
} catch (error) {
console.error('Checkout failed:', error);
}
},
handleClose() {
this.dialogVisible = false;
}
}
};
</script>
这个示例中,我们引入了Element Plus的ElDialog组件来显示一个模态对话框,当用户提交完成后会弹出一个显示"充值成功"的对话框。你也可以根据使用的框架调整具体实现。