首頁  >  問答  >  主體

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 天前447

全部回覆(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
  • 取消回覆