Inertia.js 中删除路由无法触发 onSuccess() 回调
P粉738346380
P粉738346380 2023-12-26 08:57:49
0
1
436

我有一个删除路由 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粉738346380
P粉738346380

全部回复(1)
P粉539055526

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!