首页  >  问答  >  正文

Inertia.js 中删除路由无法触发 onSuccess() 回调

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

示例.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.');

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

正确的图像

之后的情况是这样的:

图片错误

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

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

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

P粉738346380P粉738346380298 天前444

全部回复(1)我来回复

  • P粉539055526

    P粉5390555262023-12-27 00:12:17

    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
  • 取消回复