Inertia.js 未在删除路由上调用 onSuccess()

问题描述 投票:0回答:2

我有一个删除路由 Inertia.js,这样当我删除一个项目时,它会重定向回我所在的页面。然而,这并不是在 Inertia destroy 路由中调用 onSuccess() 函数。

Example.vue

deleteSubmit(id) {
    this.accountsDataTable.destroy();
    Inertia.destroy(route('installers.destroy', {id: id}), {}, { 
        preserveState: true, 
        onSuccess: () => {
            this.accountsDataTable = $('#table').DataTable({
                columnDefs: [{
                    target: 1
                }]
            });
        }
    })
},

ExampleController.php

//Validate the request
//Create the installer
//Redirect back on success
return redirect()->route('installers.index')->with('success', 'Installer was successfully deleted.');

但是,数据表没有按照我想要的方式重新创建。这是之前的样子:

正确的图像

correct image

之后就是这样的:

图片错误

wrong image

我尝试将控制器代码更改为:

return redirect()->back()->with('success', 'Installer was successfully deleted');

但是数据表仍然没有按应有的方式显示。

php laravel vue.js datatables inertiajs
2个回答
1
投票

1: 使用控制器中的消息数据设置重定向。

return redirect()->back()->with([
    'messaage' => 'Installer was successfully deleted',
])

2: HandleInertiaRequests 中间件。

public function share(Request $request)
{
    return array_merge(parent::share($request), [
        'flash' => [
            'message' => fn () => $request->session()->get('message'),
        ],
    ]);
}

3:在组件中。

<template>
{{ $page.props.flash.message }}
</template>

<script setup>
import { usePage } from '@inertiajs/inertia-vue3'

const message = usePage().props.value.flash.message
</script>

文档:https://inertiajs.com/shared-data


0
投票

我来晚了一点,但基本上,

router.delete()
方法并不采用与
router.post()
router.get()
相同的参数。所以你应该像这样构建你的请求,只有两个参数:

router.delete(route('YOUR ROUTE'), {
    onSuccess: () => {
        ...
    }
    onError: () => {
        ...
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.