如何在Inertiajs.vue中抛出来自Laravel的自定义错误而不重定向呢?
Vue组件:
Inertia.post('company-organisations-create', {
name: this.newOrganisation.name,
description: this.newOrganisation.description
},
{
preserveScroll: true,
onSuccess: (page) => {
return Promise.all([
window.Toast.success(this.$page.props.toast.message),
this.newOrganisation.name = '',
this.newOrganisation.description = '',
])
},
onError: (errors) => {
window.Toast.error(errors.toastMessage)
}
});LaravelController():
public function createOrganisations(Request $request)
{
try {
CompanyOrganisations::create([
'company_id' => $companyID,
'name' => $orgName,
'description' => $orgDescription,
]);
} catch(Excpetion) {
// Create Inertia Error 'onError'
/* As example with json response
return response()->json([
'message' => 'ups, there was an error',
], 403); */
}
return Redirect::route('company.organisations',
)->with([
'toastMessage' => 'Organization created!'
]);
}由于我不能在惯性请求中接收json格式,我需要在Inertiajs.vue组件中抛出一个错误。
非常感谢。
发布于 2021-09-05 16:56:00
试试这个:
try {
// ...
} catch(Excpetion) {
return redirect()->back()->withErrors([
'create' => 'ups, there was an error'
])
} 应接收到onError错误
onError: (errors) => {
window.Toast.error(errors.create)
}https://stackoverflow.com/questions/69065294
复制相似问题