在 Axios 调用后触发<闪烁消息 />



要从控制器激发<flash-messages />(在我的布局中(,我可以执行以下操作:

$redirect('/')->with('success', 'cart updated successfully!');

问题是,当我处理axois调用时,如果不专门去后端,我如何才能启动它??

HandleInertiaRequest(中间件(


class HandleInertiaRequests extends Middleware
{
/**
* The root template that's loaded on the first page visit.
*
* @see https://inertiajs.com/server-side-setup#root-template
* @var string
*/
protected $rootView = 'app';
/**
* Determines the current asset version.
*
* @see https://inertiajs.com/asset-versioning
* @param  IlluminateHttpRequest  $request
* @return string|null
*/
public function version(Request $request): ?string
{
return parent::version($request);
}
public function share(Request $request): array
{
return array_merge(parent::share($request), [
"flash" => function () use ($request) {
return [
"success" => $request->session()->get("success"),
"error"   => $request->session()->get("error"),
];
},
]);
}
}
addToCart() {
axios.post(`/api/v1/carts`, {
name: '....',

}).then(() => {
this.$inertia.reload({
onSuccess: page => { console.log(page) },
})
}).catch(() => {
this.$inertia.reload({
onError: errors => { console.log(errors) },
})
})
},

console.log结果

..
.
.
flash: {success: null, error: null}

我通过访问flash变量来解决它:D


addToCart() {
axios.post(`/api/v1/carts`, {
name: '....',

}).then((res) => {
this.$page.props.flash.success = 'Added to cart successfully'
}).catch((err) => {
this.$page.props.flash.error = err
})
},