Inertia.js PUT请求跳转确认问题:取消仍执行请求
解决Inertia.js跳转前确认取消不生效的问题
问题原因分析
- Inertia.put参数顺序错误:
Inertia.put的参数格式是put(url, data, options),你把包含onBefore的配置项放在了data参数的位置,导致Inertia无法识别该配置,所以确认框没弹出。 - Inertia.visit的onBefore未返回确认结果:
onBefore钩子需要返回false来终止导航,你仅调用了confirm()但未返回它的布尔值,所以无论点击确认还是取消,请求都会发送。
正确写法
写法一:修正Inertia.put的参数顺序
Inertia.put(route('disable-utente', row.id), {}, { onBefore: () => confirm('Do you really want to continue?') })
- 第二个参数传空对象(若无需提交额外数据),第三个参数才是配置项
onBefore直接返回confirm()的结果:点击确认返回true,允许请求;点击取消返回false,终止请求
写法二:修正Inertia.visit的onBefore返回值
Inertia.visit(route('disable-utente', row.id), { method: 'put', onBefore: () => confirm('Do you really want to continue?') })
- 关键是让
onBefore返回confirm()的布尔结果,以此控制是否执行请求
写法三:直接在调用前判断(更直观)
const handleDisable = () => { if (confirm('Do you really want to continue?')) { Inertia.put(route('disable-utente', row.id)) } }
- 先弹出确认框,仅在用户确认后才调用Inertia的请求方法,逻辑更清晰
内容的提问来源于stack exchange,提问作者agodoo
相关产品推荐
相关产品推荐

